python3 csv 空行解决

在使用Python3.4处理CSV文件时遇到空行问题,常规的按二进制读取方法在Python2和3中效果不一。官方文档建议在打开CSV文件时设置`newline=''`参数。按照此方法修改程序后,成功解决了空行问题。
摘要由CSDN通过智能技术生成

python3.4遇到csv读写空行问题,在网上搜索大多数给的方案是按照二进制进行读取,python2和python3在byte和str处理不太一样,没有达到效果。

查询官方文档 csv.writer,需要加入一个newline参数读取,文档截取如下

If csvfile is a file object, it should be opened with newline=”.
footnote : If newline=” is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use \r\n linendings on write an extra \r will be added. It should always be safe to specify newline=”, since the csv module does its own (universal) newline handling.

官方demo如下

import csv
with open('eggs.csv', 'w', newline='
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值