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

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

问题描述

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

I'm experimenting with python libraries for data analysis,the problem i'm facing is this exception

用户代码未处理

UnicodeDecodeError消息:'charmap'编解码器 无法解码位置165的字节0x81:字符映射为< undefined>

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

我调查了类似问题的答案,OP似乎正在读取具有不同编码的文本或将其打印出来.

I have looked into answers with similar issues and the OP seems to be either reading text with different encoding or printing it.

在我的代码中,错误显示在import语句中,这使我感到困惑.

In my code the error shows up at import statement,that's what confuses me.

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

I'm using python 64 bit 3.3 on Visual Studio 2015 and geotext is the library where it shows the error.

请指出在哪里处理此错误.

Kindly point as to where to look to deal with this error.

推荐答案

这是我解决此问题的方式(适用于

Here is how I solved this issue (its applicable for geotext 0.3.0)

检查回溯:

回溯(最近通话最近): 在第5行中输入文件"pythonTwitterTest.py" 从process.processData导入* 在第1行的文件"C:\ OwaisWorkx \ Courses \ 5th Semester \ Project \ pythonTwitterTest \ pythonTwitterTest \ process \ processData.py"中 从geotext导入GeoText#用于分类和分离城市,国家和州/省 在第7行的文件"c:\ Python33 \ lib \ site-packages \ geotext__init __.py"中 从.geotext导入GeoText 文件"c:\ Python33 \ lib \ site-packages \ geotext \ geotext.py",第87行,在 GeoText(object)类: GeoText中的文件"c:\ Python33 \ lib \ site-packages \ geotext \ geotext.py",第103行 索引= build_index() build_index中的文件"c:\ Python33 \ lib \ site-packages \ geotext \ geotext.py",第77行 城市= read_table(get_data_path('cities15000.txt'),usecols = [1,8]) read_table中的文件"c:\ Python33 \ lib \ site-packages \ geotext \ geotext.py",第54行 对于行中的行: 文件"c:\ Python33 \ lib \ site-packages \ geotext \ geotext.py",第51行,在 lines =(如果不是line.startswith(comment),则为f中的行) 解码中的文件"c:\ Python33 \ lib \ encodings \ cp1252.py",第23行 返回codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError:"charmap"编解码器无法解码位置165处的字节0x81:字符映射到

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文件中

This shows the error is actually in the geotext.py file

所以打开它

geotext.py和第45行: 更改

geotext.py and goto line 45: Change from this

with open(filename, 'r') as f

对此

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

PS: 查看全文

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