Python:将多个(未知)csvs粘贴在一起 [英] Python: `paste' multiple (unknown) csvs together
问题描述
我本质上寻找的是bash中的粘贴命令,但在Python2中。假设我有一个csv文件:
a1,b1,c1,d1
a2,b2,c2,d2
a3,b3,c3,d3
另一个例子:
e1,f1
e2,f2
e3,f3
我想把它们拉到一起:
c1,d1,e1,f1
pre>
a2,b2,c2,d2,e2,f2
a3,b3,c3,d3,e3,f3
这是最简单的情况,我有一个已知的数字,只有两个。如果我想用任意数量的文件,而不知道我有多少文件。
我正在考虑使用zip与csv列表的行。阅读器迭代。将有一些解包涉及,但似乎这样多python-foo是我的智商水平ATM。有人可以建议如何实现这个想法或完全不同的东西吗?
我怀疑这应该是可以做一个短片段。感谢。
解决方案假设文件数量未知,并且所有文件格式正确的csv具有相同数量行:
files = ['csv1','csv2','csv3']
fs = map ,files)
done = False
while not done:
chunks = []
for f in fs:
try:
l = next(f).strip()
chunks.append(l)
除了StopIteration:
done = True
break
如果未完成:
print','。join(chunks)
for f in fs:
f.close()
似乎没有简单的方法使用上下文管理器与一个可变文件列表容易,至少在Python 2中(见接受的答案在这里),因此如上所述需要手动关闭文件。
What I am essentially looking for is the `paste' command in bash, but in Python2. Suppose I have a csv file:
a1,b1,c1,d1 a2,b2,c2,d2 a3,b3,c3,d3
And another such:
e1,f1 e2,f2 e3,f3
I want to pull them together into this:
a1,b1,c1,d1,e1,f1 a2,b2,c2,d2,e2,f2 a3,b3,c3,d3,e3,f3
This is the simplest case where I have a known number and only two. What if I wanted to do this with an arbitrary number of files without knowing how many I have.
I am thinking along the lines of using zip with a list of csv.reader iterables. There will be some unpacking involved but seems like this much python-foo is above my IQ level ATM. Can someone suggest how to implement this idea or something completely different?
I suspect this should be doable with a short snippet. Thanks.
解决方案Assuming the number of files is unknown, and that all the files are properly formatted as csv have the same number of lines:
files = ['csv1', 'csv2', 'csv3'] fs = map(open, files) done = False while not done: chunks = [] for f in fs: try: l = next(f).strip() chunks.append(l) except StopIteration: done = True break if not done: print ','.join(chunks) for f in fs: f.close()
There seems to be no easy way of using context managers with a variable list of files easily, at least in Python 2 (see a comment in the accepted answer here), so manual closing of files will be required as above.
这篇关于Python:将多个(未知)csvs粘贴在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!