Screwing Up looping in Generator

Sayth Renshaw flebber.crue at gmail.com
Tue Jan 3 09:54:25 EST 2017


Hi

This is simple, but its getting me confused.

I have a csv writer that opens a file and loops each line of the file for each file and then closes, writing one file.

I want to alter the behaviour to be a written file for each input file. I saw a roundrobin example however it failed for me as you cannot get len(generator) to use a while loop on.
it exhausts

should I use the same for again after the with open?

rootobs in this code is my generator and I am already looping it however 
def data_attr(roots):
    """Get the root object and iter items."""
    for file in rootobs:
        base = os.path.basename(file.name)
        write_to = os.path.join("output", os.path.splitext(base)[0] + ".csv")
        with open(write_to, 'w', newline='') as csvf:
            race_writer = csv.writer(csvf, delimiter=',')
            race_writer.writerow(
                ["meet_id", "meet_venue", "meet_date", "meet_rail",
                 ...
                 # other categories here
                 ...
                    "jockeysurname", "jockeyfirstname"])
            for xml_data in roots:
                ...
                # parsing code
                        for noms in race_child:
                            if noms.tag == 'nomination':
                                race_writer.writerow(
                                    [meet_id, meet_venue, meet_date,
                                     ...
                                    #parsing info removed
                                        noms.get("jockeyfirstname")])

Cheers
Sayth



More information about the Python-list mailing list