我有这个csv文件(file.csv):
1,4.00,B
1,8.00,C
2,5.00,"B,C"
2,6.50,"C,D"
3,4.00,B
3,8.00,"B,D"
我想在python中读取此文件,然后在python中将此标头(ID,COST,NAME)写入此csv文件。所以它看起来像这样。
ID,COST,NAME
1,4.00,B
1,8.00,C
2,5.00,"B,C"
2,6.50,"C,D"
3,4.00,B
3,8.00,"B,D"
我该怎么做?
答案 0 :(得分:1)
这个怎么样?
headers = ["ID", "COST", "NAME"]
rows = [(1, 4.00,"B"),
(1, 8.00,"C")]
with open(name.csv','w') as f:
f_csv = csv.writer(f)
f_csv.writerow(headers)
f_csv.writerows(rows)