UnicodeDecodeError:'charmap'编解码器无法解码Y位置的字节X:字符映射到< undefined> [英] UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>

查看:1049
本文介绍了UnicodeDecodeError:'charmap'编解码器无法解码Y位置的字节X:字符映射到< undefined>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个Python 3程序用一个充满信息的文本文件做一些操作。但是,当试图读取文件时,我得到以下错误:

  Traceback(最近一次调用最后一次):
在< module>文件中的SCRIPT LOCATION文件,第NUMBER行
文本= file.read()
档 C:\Python31\lib\encodings\cp1252.py,第23行,以解码
返回codecs.charmap_decode(输入,unicode,decode_table)[0]
UnicodeDecodeError:'charmap'编码解码器无法解码位置2907500中的字节0x90:字符映射到< undefined>

如果有人能帮我试试这个问题,我会非常感激。 / p>

感谢!

解决方案

有问题的文件没有使用 CP1252 编码。它使用另一种编码。哪一个你必须弄清楚自己。常见的是 Latin-1 UTF-8 。由于 0x90 实际上并不意味着中的任何内容,所以,UTF-8 (其中 0x90 是一个延续字节)更可能。



在打开文件时指定编码:

  file = open(filename,encoding =utf8)


I'm trying to get a Python 3 program to do some manipulations with a text file filled with information. However, when trying to read the file I get the following error:

Traceback (most recent call last):
File "SCRIPT LOCATION", line NUMBER, in <module>
text = file.read()
File "C:\Python31\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 0x90 in position 2907500: character   maps to <undefined>

If anyone could give me any help to try and get past this problem I would be most grateful.

Thanks!

解决方案

The file in question is not using the CP1252 encoding. It's using another encoding. Which one you have to figure out yourself. Common ones are Latin-1 and UTF-8. Since 0x90 doesn't actually mean anything in Latin-1, UTF-8 (where 0x90 is a continuation byte) is more likely.

You specify the encoding when you open the file:

file = open(filename, encoding="utf8")

这篇关于UnicodeDecodeError:'charmap'编解码器无法解码Y位置的字节X:字符映射到&lt; undefined&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆