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=‘’。
down 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.
改写的代码如下,就能正常写入了
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)
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=‘’,你会发现也能写入结果,但是每行内容之间总是会多出一个空行
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.
down 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.
2.使用open打开要操作的
文件
,以写入模式打开 mode='w'
3.通过
csv
.
write
r(stream) --->
write
r对象
4.使用
write
r对象向
文件
中写入内容:
write
row
(['','','']),
write
row
s([[],[],[]])
5.关闭'''
import
csv
# newline="" 去除
csv
的
空行
with open('../files/cards1.
csv
', 'w',newline="") as
csv
_stream:
# 转成
write
r
def
write
_
csv
(self,path,list_data_
row
):
with open(path, ‘w’,newline=’’) as f:
csv
_
write
=
csv
.
write
r(f,dialect=‘excel’)
csv
_
write
.
write
row
(list_data_
row
)
现在是刷新,重写
如果是追加写入,将第二行的w改成a
在open()内增加
一个
参数newline=’’ 即可,代码结构如下:
with open(path, 'w', newline='')as f:
f_
csv
=
csv
.
write
r(f)
for i in tqdm(range(len(entity))):
row
= []
if i < len(entity):
csv
file = file('
csv
test.
csv
', 'wb')
write
r =
csv
.
write
r(
csv
file)
write
r.
write
row
(['id', 'url', 'keywords'])
data = [
('1', 'http://www.xiaoheiseo.com/', '小黑'),
('2', 'http:/
print(
row
Data=,
row
Data)
write
r.
write
row
(
row
Data)
path = rE:\\Python\\py17\\automatictext\\000001.
csv
write
csv
(path, [[1, 2, 3], [4, 5, 6], [7, 8, 9]
要通过binary模式去打开,即带b的,比如wb,ab+等
而不能通过文本模式,即不带b的方式,w,w+,a+等,否则,会导致使用
write
row
写内容到
csv
中时,产生对于的CR,导致多余的
空行
。
with open(self.game_name + '2022.
csv
', 'w', encoding='utf-8-sig') as f:
write
r =
csv
.
write
r(f, dialect='unix') # 将dialect设置为unix,换行符即为'\n'
write
r.
write
row
(self.bilibili_columns)
文章目录问题描述方法一:
csv
方法二: pandas
在深度学习相关任务的训练时,需要在训练的每个 epoch 记录当前 epoch 的准确率(如下图所示),那么在 python 中要怎么将内容写入
csv
文件
呢,学习发现可以使用
csv
或者 pandas 实现,在这里做个简单记录。
这里示例的代码为以追加模式写,每次写入一行
方法一:
csv
import
csv
log_path = 'log/temp.
csv
'
file = open(log_path, 'a+', encoding
unicode
csv
是Python 2.7的
csv
模块的直接替代,该模块支持unicode字符串而没有麻烦。 支持的版本是python 2.6、2.7、3.3、3.4、3.5和pypy 2.4.0。
Python 2的
csv
模块无法轻松处理unicode字符串,从而导致可怕的“'ascii'编解码器无法在位置上编码字符...”异常。
您可以通过在调用
write
之前(或在read之后)对所有内容进行编码来解决此问题,但是为什么不对串行器添加支持呢?
>>> import unicode
csv
as
csv
>>> from io import BytesIO
>>> f = BytesIO()
>>> w =
csv
.
write
r(f, encoding = ' utf-8 ' )
>>> _ = w.
write
row
(( u ' é ' , u ' ñ ' ))
如示例,用
csv
write
row
写入
文件
,会发现每写入一行,便多
一个
空行
。而我们可能并不需要这些
空行
。
本文提供2种解决方法:newline=” 和lineterminator=’\n’。
import
csv
csv
Data = [['Person', 'Age'], ['Peter', '22'], ['Jasmine', '21'], ['Sam', '24']]...
根本原因是Python版本问题python2.x中要求用‘wb’,
python3
.x中要求用'w'首先声明:
CSV
文件
是可以用二进制模式写入的python文档(传送门)关于
CSV
的
一个
用法示例:
文件
打开的mode是“wb”with open('rent.
csv
','wb') as
csv
_file:且与Pythone3里面字符串和二进制数据是两种类型,所以要将str类型转换成bytes类型出错部分...