如何将2个不同文件中的列添加到CSV python中的输出

时间:2014-04-29 10:35:57

标签: python csv

my test.csv

1,1,2
2,1,3
3,1,4

my test2.csv

2,3
2,3
2,3

如何制作output.csv:

1,1,2,2,3
2,1,3,2,3
3,1,4,2,3

所以将两个csv文件合并为一个?

这是我的代码

import csv, os, sys
with open('test.csv', 'rb') as input, open('output.csv', 'wb') as output, open ('test2.csv', 'rb') as input2:
        reader = csv.reader(input, delimiter = ',')
        reader2 = csv.reader(input2, delimiter = ',')
        writer = csv.writer(output, delimiter = ',')

        all = []                                        
        header = next(reader)
        all.append(header)
        count = 0
        for row,row2 in reader and reader2:
                count += 1
                while count:
                        all.append(row+row2)
                        break
        writer.writerows(all)

显然这不起作用,但有人知道我的目的是什么吗?

2 个答案:

答案 0 :(得分:3)

使用zip()一次迭代两个读者:

reader1 = csv.reader(input, delimiter = ',')
reader2 = csv.reader(input2, delimiter = ',')

for row1, row2 in zip(reader1, reader2):
    writer.writerow(row1 + row2)

或更短的版本:

writer.writerows(map(list.__add__, row1, row2))

如果文件很大,那么使用mapzip在Python 2中不是一个好主意,因为它们将加载两个文件中的所有行,最好是它们的迭代器存在的版本 itertools模块:itertools.imapitertools.izip

for row,row2 in reader and reader2:相当于只迭代reader2,因为and works是这样的:

>>> 1 and 2 
2
>>> 2 and 3
3
>>> 0 and 2  # returned the first falsy value, but as an iterator is not a falsy value
0            # so it will return `reader2` in your case.

<强>更新

要就地更新test2.csv,您可以使用fileinput模块,但是这样您就无法使用csv模块。

>>> import fileinput
>>> with open('test.csv') as f:
    for line in fileinput.input('test2.csv', inplace=True):
        print next(f).rstrip() + ',' + line,
...         
>>> !cat test2.csv
1,1,2,2,3
2,1,3,2,3
3,1,4,2,3

使用csv模块,您必须首先从内存中的test2.csv读取所有行,然后将新数据写入其中。

with open('test.csv') as f1, open('test2.csv', 'r+') as f2:
                                   #open in r+ mode
   reader1 = csv.reader(f1)
   rows_f2 = list(csv.reader(f2)) #read all the rows
   f2.truncate(0)                 #truncate the file
   writer = csv.writer(f2)
   writer.writerows(map(list.__add__, reader1, rows_f2))

答案 1 :(得分:0)

只需用逗号串行连接......

with open('test.csv', 'rb') as input, open('output.csv', 'wb') as output, open ('test2.csv', 'rb') as input2:
    for row, row2 in zip(input, input2):
        output.write(row.rstrip('\n') + ',' + row2)