python加入两个csv文件

标签 python csv

我有两个 .csv 文件,headers.csvcorrected.csvheaders.csv 包含所有 header ,而 corrected.csv 只是一堆有组织的数据。

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 列,而我想在 A 列中进行位移,在 B 列中加载,在 C 列中加载 cputime 等。我还想去掉 '、"、[] 和空格,所以最终结果与我上面的示例完全一样。提前致谢!

最佳答案

假设您有一行包含以逗号分隔的列名,请尝试:headers = next(csv.reader(open('headers.csv')))

关于python加入两个csv文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11786094/

相关文章:

python - 使用长整数将两个多项式 mod n,x^r-1 相乘 : what is the correct window size?

python - 尝试将列表垂直写入 .csv 文件

mysql - 在 mysql 表中创建 CSV 字段

ruby-on-rails - Ruby CSV.foreach 从特定行开始

android - HttpClient 返回错误的 csv?

python - 使用 python 获取 django 模型中字段的 "unique item"

python - 在 AWS Lambda 上运行 Selenium 时出现问题

python - 使用猫头鹰 :Class prefix with rdflib and xml serialization

python - 如何在 pyspark 列表达式中引用名称中带有连字符的列?

postgresql - Postgres CSV 复制语句