Python,UnicodeCodeError:';ascii';编解码器可以';t解码1718位置的字节0xc2:序号不在范围内(128)
我正在尝试对文件进行简单的解析,但由于特殊字符而出现错误:Python,UnicodeCodeError:';ascii';编解码器可以';t解码1718位置的字节0xc2:序号不在范围内(128),python,string,python-2.7,encoding,Python,String,Python 2.7,Encoding,我正在尝试对文件进行简单的解析,但由于特殊字符而出现错误: #!/usr/bin/env python # -*- coding: utf-8 -*-
#!/usr/bin/env python
# -*- coding: utf-8 -*-
infile = 'finance.txt'
input = open(infile)
for line in input:
if line.startswith(u'▼'):
我得到一个错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1718: ordinal not in range(128)
解决方案?您需要提供编码。例如,如果它是
utf-8
:
import io
with io.open(infile, encoding='utf-8') as fobj:
for line in fobj:
if line.startswith(u'▼'):
这适用于Python2和Python3。默认情况下,Python2在不进行编码的情况下打开文件,即读取内容将返回字节字符串。因此,您只能读取ascii
字符。在Python3中,默认值是什么
locale.getpreferredencoding(False)
在许多情况下返回utf-8
。Python 2中的标准open()
不允许指定编码。使用io.open()
可以让它经得起未来的考验,因为切换到Python3时不需要更改代码
在Python 3中:
>>> io.open is open
True
with open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
import io
with io.open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
您需要提供编码。例如,如果它是
utf-8
:
import io
with io.open(infile, encoding='utf-8') as fobj:
for line in fobj:
if line.startswith(u'▼'):
这适用于Python2和Python3。默认情况下,Python2在不进行编码的情况下打开文件,即读取内容将返回字节字符串。因此,您只能读取ascii
字符。在Python3中,默认值是什么
locale.getpreferredencoding(False)
在许多情况下返回utf-8
。Python 2中的标准open()
不允许指定编码。使用io.open()
可以让它经得起未来的考验,因为切换到Python3时不需要更改代码
在Python 3中:
>>> io.open is open
True
with open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
import io
with io.open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
使用正确的编码打开文件,例如,如果文件是用Python 3进行UTF8编码的:
>>> io.open is open
True
with open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
import io
with io.open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
在Python2中,您可以使用io.open()
(也适用于Python3):
使用正确的编码打开文件,例如,如果文件是用Python 3进行UTF8编码的:
>>> io.open is open
True
with open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
import io
with io.open('finance.txt', encoding='utf8') as f:
for line in input:
if line.startswith(u'▼'):
# whatever
在Python2中,您可以使用io.open()
(也适用于Python3):
您可以使用
编解码器
来打开带有utf-8
或utf-16
编码的文件。您也可以使用编解码器
来打开带有utf-8
或utf-16
编码的文件。从技术上讲,Python2打开文件时假定不进行编码,并返回字节字符串。Python 3打开文件时假定locale.getpreferredencoding()
(可能不是utf8
)并返回Unicode字符串,但允许指定编码。感谢您的提示。更新了我的答案。从技术上讲,Python2打开文件时假设没有编码,并返回字节字符串。Python 3打开文件时假定locale.getpreferredencoding()
(可能不是utf8
)并返回Unicode字符串,但允许指定编码。感谢您的提示。更新了我的答案。