close

[Solved] UnicodeDecode: ‘utf-8’ codec can’t decode byte

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error UnicodeDecode: ‘utf-8’ codec can’t decode byte in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How UnicodeDecode: ‘utf-8’ codec can’t decode byte Error Occurs?

Today I get the following error UnicodeDecode: ‘utf-8’ codec can’t decode byte in python.

How To Solve UnicodeDecode: ‘utf-8’ codec can’t decode byte Error ?

  1. How To Solve UnicodeDecode: 'utf-8' codec can't decode byte Error ?

    To Solve UnicodeDecode: 'utf-8' codec can't decode byte Error Alternatively: Use the open method from the codecs module to read in the file:

  2. UnicodeDecode: 'utf-8' codec can't decode byte

    To Solve UnicodeDecode: 'utf-8' codec can't decode byte Error Alternatively: Use the open method from the codecs module to read in the file:

Solution 1

str = unicode(str, errors='replace')

or

str = unicode(str, errors='ignore')

Note: This will strip out (ignore) the characters in question returning the string without them.

For me this is ideal case since I’m using it as protection against non-ASCII input which is not allowed by my application.

Alternatively: Use the open method from the codecs module to read in the file:

import codecs
with codecs.open(file_name, 'r', encoding='utf-8',
                 errors='ignore') as fdata:

Solution 2

Changing the engine from C to Python did the trick for me.

Engine is C:

pd.read_csv(gdp_path, sep='\t', engine='c')

‘utf-8’ codec can’t decode byte 0x92 in position 18: invalid start byte

Engine is Python:

pd.read_csv(gdp_path, sep='\t', engine='python')

No errors for me.

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