'UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 35: invalid start byte
I am new to Python, I am trying to read csv file using below script.
Past=pd.read_csv("C:/Users/Admin/Desktop/Python/Past.csv",encoding='utf-8')
But, getting error "UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 35: invalid start byte", Please help me to know issue here, I used encoding in script thought it will resolve error.
Solution 1:[1]
This happens because you chose the wrong encoding.
Since you are working on a Windows machine, just replacing
Past=pd.read_csv("C:/Users/.../Past.csv",encoding='utf-8')
with
Past=pd.read_csv("C:/Users/.../Past.csv",encoding='cp1252')
should solve the problem.
Solution 2:[2]
Use this solution it will strip out (ignore) the characters and return the string without them. Only use this if your need is to strip them not convert them.
with open(path, encoding="utf8", errors='ignore') as f:
Using errors='ignore'
You'll just lose some characters. but if your don't care about them as they seem to be extra characters originating from a the bad formatting and programming of the clients connecting to my socket server. Then its a easy direct solution. reference
Solution 3:[3]
Try using :
pd.read_csv("Your filename", encoding="ISO-8859-1")
The code that I parsed from some website was converted in this encoding instead of default UTF-8 encoding which is standard.
Solution 4:[4]
The following works very well for me:
encoding = 'latin1'
Solution 5:[5]
Using the code bellow works for me:
with open(keeniz_dir + '/world_cities.csv', 'r', encoding='latin1') as input:
Solution 6:[6]
Its an old question but shows up while searching for solutions to this error. So I thought to answer for all who still stumble on this thread. The encoding for the file can be checked before passing the correct value for the encoding argument. To get the encoding, a simple option in Windows is to open the file in Notepad++ and look at the encoding. The correct value for the encoding argument can then be found in the python documentation. Look at this question and the answers on stackoverflow for more details on different possibilities to get the file encoding.
Solution 7:[7]
Don't pass encoding option unless you are sure about file encoding. Default value encoding=None passes errors="replace" to open() function called. Characters with encoding errors will be substituted with replacements, you can then figure out correct encoding or just use the resulting Dataframe. If wrong encoding is provided pd will pass errors="strict" to open() and get ValueError if encoding is incorrect.
Solution 8:[8]
df = pd.read_csv( "/content/data.csv",encoding='latin1')
Just add ,encoding='latin1' and it will work
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Nitish Kumar Pal |
Solution 3 | mercury |
Solution 4 | Jason Goal |
Solution 5 | Juba Fourali |
Solution 6 | Kumar Saurabh |
Solution 7 | Jacek BÅ‚ocki |
Solution 8 | Developer-Felix |