我编写了一个Python脚本,用于合并两个csv文件,现在我想在最终的csv中添加标头。我尝试按照此处报告的建议进行操作,但出现以下错误:expected string, float found
。解决此问题的最有效方法是什么?
这是我正在使用的代码:
import csv
with open('combined_file.csv', 'w', newline='') as outcsv:
writer = csv.DictWriter(outcsv, fieldnames = ["Date", "temperature 1", "Temperature 2"])
writer.writeheader()
with open('t1.csv', 'r', newline='') as incsv:
reader = csv.reader(incsv)
writer.writerows(row + [0.0] for row in reader)
with open('t2.csv', 'r', newline='') as incsv:
reader = csv.reader(incsv)
writer.writerows(row[:1] + [0.0] + row[1:] for row in reader)