close

[Solved] UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte

Hello Guys, How are you all? Hope You all Are Fine. Today I get UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte in Python. So Here I am Explain to you all the possible solutions here.

Without Wasting your time, Lets start This Article to Solve This Error.

How This UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte Error Occurs ?

When I am doing json.dumps() Its give me following error.

  File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte

How To Solve UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte Error ?

Question: How To Solve UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte Error ?
Answer: To Solve UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte you just need to encode such strings with encode() function as follows. The error occurs because there is some non-ascii character in the dictionary and it can’t be encoded/decoded. You can also use read_csv() with unicode_escape encoding as like below.

Solution 1

The error occurs because there is some non-ascii character in the dictionary and it can’t be encoded/decoded. To solve this error you just need to encode such strings with encode() function as follows.

yourString.encode('utf-8').strip()

here yourString is string with non-ascii character.

Solution 2

Just use different codec package. You can use read_csv() with unicode_escape encoding as like below.

import pandas as pd
data = pd.read_csv(yourFilename, encoding= 'unicode_escape')

Solution 3

Just use alternative. As same like below code.

with open(yourFilePath, 'rb') as f:
  fileData = f.read()

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment