generator no iter - how do I call it from another function
Sayth Renshaw
flebber.crue at gmail.com
Sat Oct 1 10:09:23 EDT 2016
> Steve
> “Cheer up,” they said, “things could be worse.” So I cheered up, and sure
> enough, things got worse.
Loving life.
I first started with a simple with open on a file, which allowed me to use code that "iter"'s.
for example:
for meet in root.iter("meeting"):
for race in root.iter("race"):
for nom in root.iter("nomination"):
meetattr = meet.attrib
I then got correct output so wanted to scale up to passing a directory of files. So skipping the code that deals with getting it off the command line I implemented a generator to yield the file root object as needed.
This is that code
def return_files(file_list):
"""
Take a list of files and return file when called.
Calling function to supply attributes
"""
for filename in sorted(file_list):
with open(dir_path + filename) as fd:
tree = etree.parse(fd)
root = tree.getroot()
yield root
My question is though now that I have implemented it this way my
I pull in the root via a function first few lines are
def dataAttr(roots):
"""Get the root object and iter items."""
with open("output/first2.csv", 'w', newline='') as csvf:
race_writer = csv.writer(csvf, delimiter=',')
for meet in roots.iter("meeting"):
which I call as
rootObs = return_files(file_list)
dataAttr(rootObs)
So if I use a generator to pass in the root lxml object to a function how do I iter since python provides an error that iters don't exist on python objects?
This is said error
± |master U:1 ?:1 ✗| → python3 race.py data/ -e *.xml
Traceback (most recent call last):
File "race.py", line 77, in <module>
dataAttr(rootObs)
File "race.py", line 55, in dataAttr
for meet in roots.iter("meeting"):
AttributeError: 'generator' object has no attribute 'iter'
Cheers
Sayth
More information about the Python-list
mailing list