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)```
zip(). Something similar tofor oldrow1, oldrow2 in zip(odlfile1, oldfile2): write_new_line()[oldrow2 + oldfile1[i] for i in oldfile1_indices]it connectsoldrow2with every line in oldfile1 - instead one line it create many lines.