Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python 3.4.2中读取文件时出现编解码器错误_Python 3.x - Fatal编程技术网

Python 3.x 在python 3.4.2中读取文件时出现编解码器错误

Python 3.x 在python 3.4.2中读取文件时出现编解码器错误,python-3.x,Python 3.x,我正在使用Python3.4.2,我看到了下面的错误,我已经包含了编码类型utf-8,但仍然看到了错误 File "C:\Python34(1)\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0

我正在使用Python3.4.2,我看到了下面的错误,我已经包含了编码类型utf-8,但仍然看到了错误

File "C:\Python34(1)\lib\encodings\cp1252.py", line 23, in decode     
   return codecs.charmap_decode(input,self.errors,decoding_table)[0]
   UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 6389:
   character maps to <undefined>
with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1
我的文件中的行
916355
916356
如下所示

with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1
您尚未“包含编码类型utf-8”。如果包含,您的代码将如下所示:

with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1
with open(filename, encoding='utf8') as infile:  # Put the encoding here
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)
print(lc.encode('ascii', errors='backslashescape').decode('ascii'))
但这还不够。不幸的是,Windows终端不能使用完整的Unicode。它们通常可以一次处理一个代码页,但不能一次处理全部Unicode

with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1
如果这只是为了调试,您可以尝试以下操作:

with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1
with open(filename, encoding='utf8') as infile:  # Put the encoding here
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)
print(lc.encode('ascii', errors='backslashescape').decode('ascii'))
将Unicode字符转换为反斜杠转义序列(
\u1234
=U+1234),并将ASCII字符传递给未更改的用户

with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1

如果您真的关心这个输出,那么您需要决定要使用什么编码以及如何输出它。写入UTF-8格式的文件可能更容易,然后使用支持Unicode的文本编辑器打开该文件(可以在“打开”对话框中告知记事本以UTF-8格式打开文件)。

请显示对实际读取进行编码的代码,因为错误清楚地表明仍使用系统代码页读取该文件(CP1252).Hi-dhke谢谢您的快速响应,我猜上面已经添加了代码信息,但是您的代码中的第23行是带有函数
print()
的吗?如果是这样:尝试打印(lc.encode(sys.stdout.encoding,'ignore'))并检查这是否有帮助。
with open(filename) as infile:
    lc = 0
    for line in infile:
        lc = lc +1
        print (lc)                                                                                                     
2015 Jun 30 05:06:09.073049 igmp [7214]: : Received General v2 Query from 40.150.3.1 (Eth3/1), mrt: 10 sec
2015 Jun 30 05:06:11.429282 igmp [7214]: : Send v2 Report for 224.0.1.39 on Eth3/1