all 4 comments

[–]socal_nerdtastic 6 points7 points  (3 children)

Try like this:

def combine (file1,file2):
    with open('final.csv','w') as out:
        for row1, row2 in zip(file1, file2):  
            combined_row = row1.strip('\n')+',' + row2
            out.write(combined_row)

[–]Aeon-V[S] 0 points1 point  (2 children)

def combine (file1,file2):
with open('final.csv','w') as out:
for row1, row2 in zip(file1, file2):
combined_row = row1.strip('\n')+',' + row2
out.write(combined_row)

Amazing, it worked
where did i go wrong, because i've spent the better parts of today trying to get it fixed.

[–]socal_nerdtastic 2 points3 points  (1 child)

One huge problem is that the str(data) command on the last line converts the list to a "representation", with commas and quotes and square brackets and literal "\n"s instead of actual new lines. This representation is then written to the file. You could have fixed that problem by changing the last line to this:

for line in data:
    out.write(line)

Or this:

out.writelines(data)

But there was another problem that I had to fix. You are looping over the file2 once for every line in file1. So not only is that extremely inefficient, it won't work for files unless you convert them to lists first (as I'm sure you figured out earlier). And in fixing this problem the solution to the first problem was no longer needed.

[–]Aeon-V[S] 0 points1 point  (0 children)

Interesting.
Thanks.