阅读UTF-8文件与IronPython的编解码器(Reading UTF-8 file with

2019-07-28 22:31发布

我在UTF-8编码的.csv文件,其中包含拉丁文和西里尔符号。

;F1;F2;abcdefg3;F200
;ABSOLUTE;NOMINAL;NOMINAL;NOMINAL
o1;1;USA;Новосибирск;1223

我想在IronPython的2.7.1执行下面的脚本:

import codecs

f = codecs.open(r"file.csv", "rb", "utf-8")
f.next()

f.next的执行过程中()发生异常:

Traceback (most recent call last):
  File "c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Python Tools for Visual Studio\1.1\visualstudio_py_repl.py", line 492, in run_file_as_main
    code.Execute(self.exec_mod)
  File "<string>", line 4, in <module>
  File "C:\Program Files\IronPython 2.7.1\Lib\codecs.py", line 684, in next
    return self.reader.next()
  File "C:\Program Files\IronPython 2.7.1\Lib\codecs.py", line 615, in next
    line = self.readline()
  File "C:\Program Files\IronPython 2.7.1\Lib\codecs.py", line 530, in readline
    data = self.read(readsize, firstline=True)
  File "C:\Program Files\IronPython 2.7.1\Lib\codecs.py", line 477, in read
    newchars, decodedbytes = self.decode(data, self.errors)
UnicodeEncodeError: ('unknown', '\x00', 0, 1, '')

在CPython的2.7,同时该脚本正常工作。 另外,在IronPython的2.7.1下面的脚本正常工作:

import codecs

f = codecs.open(r"file.csv", "rb", "utf-8")
f.readlines()

有谁知道什么可能造成这种奇怪的行为?

Answer 1:

看起来可能是怎样的一个错误next()处理的编解码器。 能否请您打开一个问题与文件附再现范围?



Answer 2:

可能是“RB”参数的麻烦,尝试用“R”

f = codecs.open(r"file.csv", "r", "utf-8")


文章来源: Reading UTF-8 file with codecs in IronPython