close

[Solved] UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position 1

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

Today I get the following error UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position 1 in python.

How To Solve UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xef in position 1 Error ?

  1. How To Solve UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1 Error ?

    To Solve UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1 Error If you are working on a remote host, look at /etc/ssh/ssh_config on your local PC.

  2. UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1

    To Solve UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 1 Error If you are working on a remote host, look at /etc/ssh/ssh_config on your local PC.

Solution 1

If you are working on a remote host, look at /etc/ssh/ssh_config on your local PC.

When this file contains a line:

SendEnv LANG LC_*

comment it out with adding # at the head of line. It might help.

With this line, ssh sends language related environment variables of your PC to the remote host. It causes a lot of problems.

Solution 2


This is to do with the encoding of your terminal not being set to UTF-8. Here is my terminal

$ echo $LANG
en_GB.UTF-8
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
(。・ω・。)ノ
>>> 

On my terminal the example works with the above, but if I get rid of the LANG setting then it won’t work

$ unset LANG
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> print s1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
>>> 

Consult the docs for your linux variant to discover how to make this change permanent.

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