简体   繁体   中英

How to write a CSV file combining 2 other CSV rows (Python)

I'm trying to analyze some data, and to do so I am creating a new CSV file by writing some rows which are composed from other CSV files. I've extracted the data from one of the files ( oldfile1 ) so it's a list (with specific indices I'm using to append to the new file), but the other ( oldfile2 ) I'm using for the base of the file, so I can directly add the rows from that file, as they need no filtering. The formula for a new line should be row from oldfile2 + row from oldfile1 . first is intend to skip the comment line. However, this code currently creates a hilariously large output file (200MB)--I suspect that it is looping through multiple times per row, duplicating the written rows. However, I cannot immediately think of another way to ensure the rows from oldfile2 are looped through while not duplicating the written rows. I also cannot give much more detail on the output file as it crashes whenever I try to open it. Any help appreciated.

with open('newfile.csv','w+') as f:
        reader = csv.reader(open('oldfile2.csv'), delimiter=',')
        writer = csv.writer(f, delimiter=',')
        first = next(reader)
        for oldrow2 in reader:
                outline = [oldrow2 + oldfile1[i] for i in oldfile1_indices]
                writer.writerow(outline)```


I can't test it but I think you need zip() to create pairs (oldrow2, i) and then create new row and save it

oldfile1 = list(csv.reader(open('oldfile1.csv'), delimiter=','))
oldfile1_indices = [...]

with open('newfile.csv','w+') as f:
    writer = csv.writer(f, delimiter=',')

    reader2 = csv.reader(open('oldfile2.csv'), delimiter=',')
    next(reader2)

    for oldrow2, i in zip(reader2, oldfile1_indices):
        outline = [oldrow2 + oldfile1[i]]
        writer.writerow(outline)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM