问题描述
假设我在 Python $中有两个CSV文件 A 和 B c $ c>。
Suppose I have two CSV files called A and B in Python.
A 的头看起来像:
headerNameA1,headerNameA2 1.12412424,1 1,1 1,1 1,1
B head 如下所示:
headerNameB1,headerNameB2 1,1 1,1 1,1 1,1
$ b b
我的目标是将这两个组合成一个文件 C 。不过,我的目标是不要将它们加载到RAM ,因为它们是大文件。所产生的 C 将是:
My objective is to combine the two into a file C. However, my objective is also not to load them into RAM since they're large files. The resulting C would be:
headerNameA1,headerNameA2,headerNameB1,headerNameB2 1.12412424,1,1,1 1,1,1,1 1,1,1,1 1,1,1,1
如果解决方案可以合并多个CSV文件,则会增加积分。但是,如果在解决方案中这是不可能的,那是很好的,因为我总是可以与 C 组合以使 D ,然后结合 D 以使 E ,无限制。
Bonus points if the solution can combine multiple CSV files. However, it is fine if this is not possible in the solution since I can always combine whatever else with C to make D, then combine whatever else with D to make E, ad infinitum.
解决方案可以是基于Python 的或终端通过 中的 os.system 。
The solution can be Python based or terminal based through os.system in Python.
推荐答案
您可以从两个文件一次使用一行,将它们连接在一起并写入您的outfile。 csv 模块使事情更清晰。
You can consume one line at a time from both files, concatenating them together and writing to your outfile. The csv module makes things a bit cleaner.
import csv with open('A','rb') as f1, open('B','rb') as f2, open('out.csv','wb') as w: writer = csv.writer(w) r1,r2 = csv.reader(f1),csv.reader(f2) while True: try: writer.writerow(next(r1)+next(r2)) except StopIteration: break
作为@RogerPate指出,你可以使这个slicker与 itertools.izip (只是 zip 如果你在python3 )
And as @RogerPate points out, you can make this slicker with itertools.izip (just zip if you're in python3)
from itertools import izip import csv with open('A','rb') as f1, open('B','rb') as f2, open('out.csv','wb') as w: writer = csv.writer(w) for r1,r2 in izip(csv.reader(f1),csv.reader(f2)): writer.writerow(r1+r2)
这篇关于按列组合CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!