close

[Solved] UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 13: ordinal not in range(128)

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 13: ordinal not in range(128) 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 UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 13: ordinal not in range(128) Error Occurs?

Today I get the following error UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 13: ordinal not in range(128) in python.

How To Solve UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe2 in position 13: ordinal not in range(128) Error ?

  1. How To Solve UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) Error ?

    To Solve UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) Error The file is being read as a bunch of strs, but it should be unicodes. Python tries to implicitly convert, but fails. Change:

  2. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)

    To Solve UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128) Error The file is being read as a bunch of strs, but it should be unicodes. Python tries to implicitly convert, but fails. Change:

Solution 1

I got this error when trying to install a python package in a Docker container. For me, the issue was that the docker image did not have a locale configured. Adding the following code to the Dockerfile solved the problem for me.

# Avoid ascii errors when reading files in Python
RUN apt-get install -y locales && locale-gen en_US.UTF-8
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'

Solution 2

The file is being read as a bunch of strs, but it should be unicodes. Python tries to implicitly convert, but fails. Change:

job_titles = [line.strip() for line in title_file.readlines()]

to explicitly decode the strs to unicode (here assuming UTF-8):

job_titles = [line.decode('utf-8').strip() for line in title_file.readlines()]

It could also be solved by importing the codecs module and using codecs.open rather than the built-in open.

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