[英]How can I add columns from 2 different files to an output in CSV python
我的test.csv
1,1,2
2,1,3
3,1,4
我的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)
显然这不起作用,但有人知道我想要的是什么吗?
使用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))
如果文件很大,那么使用map
, zip
在Python 2中不是一个好主意,因为它们将加载来自两个文件的所有行,更好的是它们在itertools模块中存在的迭代器版本: itertools.imap
和itertools.izip
:
for row,row2 in reader and reader2:
相当于只reader2
因为and
工作原理如下:
>>> 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))
只需用逗号连续逐行连接......
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.