How To Solve The Error: UnicodeDecodeError: ‘utf-8’ Codec Can’t Decode Byte 0x92 In Position In Python

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position in python

This article can help you learn how to solve the UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x92 in position in Python. Let’s follow this article to learn more about it with the explanation and examples below.

How does the UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x92 in position In Python happen?

The error occurs when you assign the wrong encoding while you are decoding the bytes object.

Look at the example below to learn more about this error.

byte = 'LearnShareIT ’ crvt4722'.encode('cp1252')
str = byte.decode('utf-8') # The Error will occur here
print(str)

Output

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 13: invalid start byte.

How to solve this error?

These are some solutions that can help you solve the error. You have to set the same encoding which is used to encode the string while you are decoding the bytes object. You can also ignore the error to fix the problem. Another solution is using the encoding =”ISO-8859-1”.

Set the same encoding 

You have to set the same encoding which is used to encode the string while you are decoding the bytes object.

Look at the example below to learn more about this solution.

byte = 'LearnShareIT ’ crvt4722'.encode('cp1252')
str = byte.decode('cp1252')
print(str)

Output

LearnShareIT ’ crvt4722

Ignore the error

If the error occurs, you ignore the characters that can not be decoded by the error keyword.

Look at the example below to learn more about this solution.

with open(file_path,encoding= 'utf-8', errors= 'ignore') as f:
    content = f.readlines()
    print(content)

Use the encoding = “ISO-8859-1”

You can use the encoding ISO-8859-1 to solve the error.

Look at the example below to learn more about this solution.

with open(file_path,encoding= 'ISO-8859-1') as f:
    content = f.readlines()
    print(content)

Summary

These are some solutions that can help you solve the UnicodeDecodeError: ‘utf-8’ codec can’t decode byte 0x92 in position in Python. To solve this problem, you have to set the same encoding which is used to encode the string while you are decoding the bytes object. You can ignore the error or use the encoding ISO-8859-1. Choose the solution that is the most suitable for you. We hope this tutorial is helpful to you. Thanks!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *