问题:csv.Error:迭代器应返回字符串,而不是字节

Sample.csv包含以下内容:

NAME    Id   No  Dept
Tom     1    12   CS
Hendry  2    35   EC
Bahamas 3    21   IT
Frank   4    61   EE

Python文件包含以下代码:

import csv
ifile  = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
    print (row) 

当我在Python中运行上述代码时,出现以下异常:

文件“ csvformat.py”,第4行,在已读行中表示:_csv.Error:迭代器应返回字符串,而不是字节(您是否以文本模式打开文件?)

我该如何解决?

Sample.csv contains the following:

NAME    Id   No  Dept
Tom     1    12   CS
Hendry  2    35   EC
Bahamas 3    21   IT
Frank   4    61   EE

And the Python file contains the following code:

import csv
ifile  = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
    print (row) 

When I run the above code in Python, I get the following exception:

File “csvformat.py”, line 4, in for row in read : _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

How can I fix it?


回答 0

您以文本模式打开文件。

进一步来说:

ifile  = open('sample.csv', "rt", encoding=<theencodingofthefile>)

编码的不错猜测是“ ascii”和“ utf8”。您还可以关闭编码,它将使用系统默认编码,该编码通常为UTF8,但可能还有其他含义。

You open the file in text mode.

More specifically:

ifile  = open('sample.csv', "rt", encoding=<theencodingofthefile>)

Good guesses for encoding is “ascii” and “utf8”. You can also leave the encoding off, and it will use the system default encoding, which tends to be UTF8, but may be something else.


回答 1

我只是用我的代码解决了这个问题。引发该异常的原因是因为您有参数rb。更改为r

您的代码:

import csv
ifile  = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
    print (row) 

新代码:

import csv
ifile  = open('sample.csv', "r")
read = csv.reader(ifile)
for row in read :
    print (row)

The reason it is throwing that exception is because you have the argument rb, which opens the file in binary mode. Change that to r, which will by default open the file in text mode.

Your code:

import csv
ifile  = open('sample.csv', "rb")
read = csv.reader(ifile)
for row in read :
    print (row) 

New code:

import csv
ifile  = open('sample.csv', "r")
read = csv.reader(ifile)
for row in read :
    print (row)

回答 2

您的问题是您bopen标志中。该标志rt(读取,文本)是默认设置,因此,使用上下文管理器只需执行以下操作:

with open('sample.csv') as ifile:
    read = csv.reader(ifile) 
    for row in read:
        print (row)  

上下文管理器意味着您不需要常规的错误处理(如果没有这种处理,您可能会卡在打开文件中,尤其是在解释器中),因为它会在发生错误或退出上下文时自动关闭文件。

上面是一样的:

with open('sample.csv', 'r') as ifile:
    ...

要么

with open('sample.csv', 'rt') as ifile:
    ...

Your problem is you have the b in the open flag. The flag rt (read, text) is the default, so, using the context manager, simply do this:

with open('sample.csv') as ifile:
    read = csv.reader(ifile) 
    for row in read:
        print (row)  

The context manager means you don’t need generic error handling (without which you may get stuck with the file open, especially in an interpreter), because it will automatically close the file on an error, or on exiting the context.

The above is the same as:

with open('sample.csv', 'r') as ifile:
    ...

or

with open('sample.csv', 'rt') as ifile:
    ...

回答 3

csv.reader期望在Python3中,传递可迭代的返回字符串,而不是字节。这是使用codecs模块的另一解决方案:

import csv
import codecs
ifile  = open('sample.csv', "rb")
read = csv.reader(codecs.iterdecode(ifile, 'utf-8'))
for row in read :
    print (row) 

In Python3, csv.reader expects, that passed iterable returns strings, not bytes. Here is one more solution to this problem, that uses codecs module:

import csv
import codecs
ifile  = open('sample.csv', "rb")
read = csv.reader(codecs.iterdecode(ifile, 'utf-8'))
for row in read :
    print (row) 

回答 4

运行使用Python 2.6.4开发的旧python脚本时出现此错误

更新到3.6.2时,我必须从打开的调用中删除所有“ rb”参数,以修复此csv读取错误。

I had this error when running an old python script developped with Python 2.6.4

When updating to 3.6.2, I had to remove all ‘rb’ parameters from open calls in order to fix this csv reading error.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。