python charmap编解码器无法将位置Y字符映射中的字节X解码为<undefined>

2024-04-16 18:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用python库进行数据分析,我面临的问题是这个异常

UnicodeDecodeError was unhandled by user code Message: 'charmap' codec can't decode byte 0x81 in position 165: character maps to < undefined>

我已经研究过类似问题的答案,操作系统似乎要么读取不同编码的文本,要么打印文本。

在我的代码中,错误出现在import语句中,这让我很困惑。 enter image description here

我在Visual Studio 2015上使用python 64位3.3 geotext是显示错误的库。

请指出在哪里处理这个错误。


Tags: in文本messageby错误codebytecan
1条回答
网友
1楼 · 发布于 2024-04-16 18:27:17

下面是我如何解决这个问题的(它适用于geotext 0.3.0

检查回溯:

Traceback (most recent call last): File "pythonTwitterTest.py", line 5, in from process.processData import * File "C:\OwaisWorkx\Courses\5th Semester\Project\pythonTwitterTest\pythonTwitterTest\process\processData.py", line 1, in from geotext import GeoText # for classifying and seperating City , Country and States/Provinces File "c:\Python33\lib\site-packages\geotext__init__.py", line 7, in from .geotext import GeoText File "c:\Python33\lib\site-packages\geotext\geotext.py", line 87, in class GeoText(object): File "c:\Python33\lib\site-packages\geotext\geotext.py", line 103, in GeoText index = build_index() File "c:\Python33\lib\site-packages\geotext\geotext.py", line 77, in build_index cities = read_table(get_data_path('cities15000.txt'), usecols=[1, 8]) File "c:\Python33\lib\site-packages\geotext\geotext.py", line 54, in read_table for line in lines: File "c:\Python33\lib\site-packages\geotext\geotext.py", line 51, in lines = (line for line in f if not line.startswith(comment)) File "c:\Python33\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 0x81 in position 165: character maps to

这表明错误实际上在geotext.py文件中

把它打开

geotext.py和goto第45行: 从此改变

with open(filename, 'r') as f

为了这个

with open(filename, 'r', encoding='utf-8') as f:

附言:Solution taken from Python-forum.io

相关问题 更多 >