Python3 写入csv文件每行数据后都多一空行 问题解决
Python3处理好的数据导出csv文件,需要做到以下两点才能正确处理:
1. 以w模式而不是wb模式打开
2. 加上newline=”
正确代码如下:
# 导出csv文件代码片段:
with open('mails.csv', 'w', newline='') as csvfile:
wresource = csv.writer(csvfile, dialect='excel')
wresource.writerow(['发件时间', '发件人', '邮件主题'])
wresource.writerows(rs)
原因分析:
newline=” suppresses text mode newline handling. On Windows, failing to do this will write \r\r\n file line endings instead of the correct \r\n. This is mentioned in the 3.X csv.reader documentation only, but csv.wr[……]