您的位置:首页 > 编程语言 > Python开发

python3 writerow csv文件多一个空行

2017-08-03 13:48 701 查看
转自 http://blog.csdn.net/pfm685757/article/details/47806469
用 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='')



encoding
 can
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='')



encoding
 can
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.

如果不带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 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='')



encoding
 can
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.

下面是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.

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='')



encoding
 can
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.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: