python加入两个csv文件 [英] python joining two csv files
问题描述
我有两个.csv文件,分别是 headers.csv
和 corrected.csv
。 headers.csv
具有所有标题,而 corrected.csv
只是一堆有组织的数据。
I have two .csv files, headers.csv
and corrected.csv
. Theheaders.csv
has all the headers, and the corrected.csv
is just a bunch of organized data.
headers.csv:
displacement, load, cputime, ...
corrected.csv:
-990.478170,-0.000026,15:08:06, ...
-990.038170,-0.000026,15:08:06, ...
The end goal is to be like this example:
displacement,load,cputime, ...
-990.478170,-0.000026,15:08:06, ...
-990.038170,-0.000026,15:08:06, ...
我拥有什么:
headers = [x for x in csv.reader(open('headers.csv', 'rb'))]
writer = csv.writer(open('merged.csv', 'wb'))
writer.writerow(headers)
for row in csv.reader(open('corrected.csv', 'rb')):
writer.writerow(row)
但是结果是 [['displacement','load','cputime',...]
全部写入列有一阵子我想要A列中的位移,B列中的负载,C列中的cputime等。我还想摆脱',,[]和空白
因此最终结果与我上面的示例完全一样。
The result, however, is that "['displacement', 'load', 'cputime', ...]"
is all written to column A while I want displacement in column A, load in column B, cputime in column C, etc. I also want to get rid of the ', ", [], and whitespace
so the end result is exactly like my example above. Thanks in advance!
推荐答案
假设您有一行用逗号分隔的列名,请尝试:标头= next(csv.reader(open('headers.csv')))
Assuming that you have a single row with comma delimited column names, try: headers = next(csv.reader(open('headers.csv')))
这篇关于python加入两个csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!