python charmap codec cant decode byte X in position Y character maps to undefined

2024/9/20 15:20:47

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

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

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

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

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.

Answer

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

Check the traceback:

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/ProvincesFile "c:\Python33\lib\site-packages\geotext__init__.py", line 7, in from .geotext import GeoTextFile "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 GeoTextindex = build_index()File "c:\Python33\lib\site-packages\geotext\geotext.py", line 77, in build_indexcities = read_table(get_data_path('cities15000.txt'), usecols=[1, 8])File "c:\Python33\lib\site-packages\geotext\geotext.py", line 54, in read_tablefor 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 decodereturn codecs.charmap_decode(input,self.errors,decoding_table)[0]UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 165: character maps to

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

so open that up

geotext.py and goto line 45: Change from this

with open(filename, 'r') as f

to this

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

P.S: Solution taken from Python-forum.io

https://en.xdnf.cn/q/72330.html

Related Q&A

get icloud web service endpoints to fetch data

My question may look silly but I am asking this after too much search on Google, yet not have any clue.I am using iCloud web services. For that I have converted this Python code to PHP. https://github.…

How to put result of JavaScript function into python variable. PyQt

I want to make a function in PyQt evaluateJavaScript() (or may be similar one) and than display a result of evaluated function. Real function will be much bigger, and it might not be a string.Im only …

Wrap multiple tags with BeautifulSoup

Im writing a python script that allow to convert a html doc into a reveal.js slideshow. To do this, I need to wrap multiple tags inside a <section> tag. Its easy to wrap a single tag inside anoth…

How to permanently delete a file in python 3 and higher?

I want to permanently delete a file i have created with my python code. I know the os.remove() etc but cant find anything specific to delete a file permanently.(Dont want to fill Trash with unused file…

Django. Python social auth. create profiles at the end of pipeline

I want to add a function at the end of the auth pipeline, the function is meant to check if there is a "Profiles" table for that user, if there isnt it will create a table. The Profiles mode…

What is a good audio library for validating files in Python?

Im already checking for content-type, size, and extension (Django (audio) File Validation), but I need a library to read the file and confirm that it is in fact what I hope it is (mp3 and mp4 mostly).I…

Python 3.6+: Nested multiprocessing managers cause FileNotFoundError

So Im trying to use multiprocessing Manager on a dict of dicts, this was my initial try:from multiprocessing import Process, Managerdef task(stat):test[z] += 1test[y][Y0] += 5if __name__ == __main__:te…

Convert python disassembly from dis.dis back to codeobject

Is there any way to create code object from its disassembly acquired with dis.dis?For example, I compiled some code using co = compile(print("lol"), <string>, exec) and then printed di…

Loop over a tensor and apply function to each element

I want to loop over a tensor which contains a list of Int, and apply a function to each of the elements. In the function every element will get the value from a dict of python. I have tried the easy wa…

How to quickly get the last line from a .csv file over a network drive?

I store thousands of time series in .csv files on a network drive. Before I update the files, I first get the last line of the file to see the timestamp and then I update with data after that timestamp…