[英]Writing header to a csv file in python
I have this csv file ( file.csv) : 我有这个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"
I would like to read this file in python and then write a header( ID, COST, NAME) to this csv file in python. 我想在python中读取此文件,然后在python中将此csv文件写入标头(ID,COST,NAME)。 So that it looks like this.
这样看起来像这样。
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"
How can I do this ? 我怎样才能做到这一点 ?
What about this? 那这个呢?
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.