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

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

问题描述

我正在尝试让 Python 3 程序对填充了信息的文本文件进行一些操作.但是,在尝试读取文件时出现以下错误:

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:Python31libencodingscp1252.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>`  

推荐答案

有问题的文件未使用 CP1252 编码.它正在使用另一种编码.哪一种你自己想办法.常见的有Latin-1UTF-8.由于 0x90Latin-1 中实际上没有任何意义,UTF-8(其中 0x90 是一个延续字节)更有可能.

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天全站免登陆