csv.write写入csv时跳过行

编程入门 行业动态 更新时间:2024-10-27 18:18:30
本文介绍了csv.write写入csv时跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正试图通过以下

写入csv文件:

file = open('P:\test。 csv','a') fieldnames =('ItemID','Factor','FixedAmount') wr = csv.DictWriter(file,fieldnames = fieldnames) header = dict((n,n)for n in fieldnames) wr.writerow(headers) wr.writerow({'ItemID':1,'Factor':2, FixedAmount':3})

但是,当我看csv文件时,第一行是空的,第二行是我的头,第三行是空的,第四个ow显示条目1,2和3.为什么它在跳过一行之前,它做一个条目?

编辑:使用python 3.2

解决方案

解决方案是在构造函数中指定lineterminator p>

file = open('P:\test.csv','w') fields = ('ItemID','Factor','FixedAmount') wr = csv.DictWriter(file,fieldnames = fields,lineterminator ='\\\') wr.writeheader wr.writerow({'ItemID':1,'Factor':2,'FixedAmount':3}) file.close() pre>

I am trying to write to a csv file via the following

file = open('P:\test.csv', 'a') fieldnames = ('ItemID', 'Factor', 'FixedAmount') wr = csv.DictWriter(file, fieldnames=fieldnames) headers = dict((n, n) for n in fieldnames) wr.writerow(headers) wr.writerow({'ItemID':1, 'Factor': 2, 'FixedAmount':3})

However, when I look at the csv-file the first row is empty, the second row is my header, the third row is empty again and the fourth ow shows the entries 1,2 and 3. why does it >skip one row before it makes an entry?

EDIT: using python 3.2

解决方案

Solution is to specify the "lineterminator" parameter in the constructor:

file = open('P:\test.csv', 'w') fields = ('ItemID', 'Factor', 'FixedAmount') wr = csv.DictWriter(file, fieldnames=fields, lineterminator = '\n') wr.writeheader() wr.writerow({'ItemID':1, 'Factor': 2, 'FixedAmount':3}) file.close()

更多推荐

csv.write写入csv时跳过行

本文发布于:2023-10-26 01:05:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1528628.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳过   csv   write

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!