python3 writerow csv文件多一个空行
2017-08-03 13:48
701 查看
转自 http://blog.csdn.net/pfm685757/article/details/47806469
用 python3写代码的时候发现, 使用下面的 wb打开文件,
.
.
.
到这里用 writerow写入内容的时候 会报错,如下 :
writer.writerow(value)
TypeError: 'str' does not support the buffer interface
在stackoverflow上找到了比较经典的解释,原来 python3里面对 str和bytes类型做了严格的区分,不像python2里面某些函数里可以混用。所以用python3来写wirterow时,打开文件不要用wb模式,只需要使用w模式,然后带上newline=‘’。
改写的代码如下,就能正常写入了
.
.
.
如果不带newline=‘’,你会发现也能写入结果,但是每行内容之间总是会多出一个空行
下面是python参考手册里的解释,在windows这种使用\r\n的系统里,不用newline=‘’的话,会自动在行尾多添加个\r,导致多出一个空行,即行尾为\r\r\n
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.
用 python3写代码的时候发现, 使用下面的 wb打开文件,
writefile = open('result.csv','wb') writer = csv.writer(writefile)
.
.
.
rvalue = self.traceprocess(item[0],item[1],item[6]) print(rvalue) if rvalue: writer.writerow(value)
到这里用 writerow写入内容的时候 会报错,如下 :
writer.writerow(value)
TypeError: 'str' does not support the buffer interface
在stackoverflow上找到了比较经典的解释,原来 python3里面对 str和bytes类型做了严格的区分,不像python2里面某些函数里可以混用。所以用python3来写wirterow时,打开文件不要用wb模式,只需要使用w模式,然后带上newline=‘’。
3down vote | In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling. In python 3.X, the csv module still does its own 4000 line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is: outputfile=open("out.csv",'w',encoding='utf8',newline='') encodingcan be whatever you require, but 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.writer requires it as well. |
writefile = open('result.csv','w',newline =‘’) writer = csv.writer(writefile)
.
.
.
rvalue = self.traceprocess(item[0],item[1],item[6]) print(rvalue) if rvalue: writer.writerow(value)
3down vote | In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling. In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is: outputfile=open("out.csv",'w',encoding='utf8',newline='') encodingcan be whatever you require, but 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.writer requires it as well. |
3down vote | In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling. In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is: outputfile=open("out.csv",'w',encoding='utf8',newline='') encodingcan be whatever you require, but 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.writer requires it as well. |
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.
3down vote | In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling. In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is: outputfile=open("out.csv",'w',encoding='utf8',newline='') encodingcan be whatever you require, but 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.writer requires it as well. |
相关文章推荐
- python3 writerow CSV文件多一个空行
- python3 writerow CSV文件多一个空行
- python3中使用csv模块的csv.writerow()产生的空行问题
- Python中通过csv的writerow输出的内容有多余的空行
- Python 中通过csv的writerow输出的内容有多余空行的解决办法
- Python中通过csv的writerow输出的内容有多余的空行
- python读取CSV文件以及“写入CSV致文件每一行后面会多一个空行”问题解决
- csv的writerow写入的文件会有空行很蛋疼
- python读取csv文件并把文件放入一个list中脚本实例
- Python读写CSV文件--追加方式多空行
- Python之一个关于天气的csv文件中获取最高和最低气温的图表
- python使用writerows写csv文件产生多余空行
- python读取csv文件并把文件放入一个list中脚本实例
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- 关于python3中写入csv文件多余空行
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- Python将一个CSV文件里的数据追加到另一个CSV文件的方法
- python读取csv文件并把文件放入一个list中脚本实例
- Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题
- Python中通过csv的writerow输出多余空行+py2.x &py3.兼容