close

[Solved] How to fix: “UnicodeDecode: ‘ascii’ codec can’t decode byte”

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error How to fix: “UnicodeDecode: ‘ascii’ 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 to fix: “UnicodeDecode: ‘ascii’ codec can’t decode byte” Error Occurs?

Today I get the following error How to fix: “UnicodeDecode: ‘ascii’ codec can’t decode byte” in python.

How To Solve fix: “UnicodeDecode: ‘ascii’ codec can’t decode byte” Error ?

  1. How To Solve fix: “UnicodeDecode: 'ascii' codec can't decode byte” Error ?

    To Solve fix: “UnicodeDecode: 'ascii' codec can't decode byte” Error This is the classic “unicode issue”. I believe that explaining this is beyond the scope of a StackOverflow answer to completely explain what is happening.

  2. How to fix: “UnicodeDecode: 'ascii' codec can't decode byte”

    To Solve fix: “UnicodeDecode: 'ascii' codec can't decode byte” Error This is the classic “unicode issue”. I believe that explaining this is beyond the scope of a StackOverflow answer to completely explain what is happening.

Solution 1


This is the classic “unicode issue”. I believe that explaining this is beyond the scope of a StackOverflow answer to completely explain what is happening.

It is well explained here.

In very brief summary, you have passed something that is being interpreted as a string of bytes to something that needs to decode it into Unicode characters, but the default codec (ascii) is failing.

The presentation I pointed you to provides advice for avoiding this. Make your code a “unicode sandwich”. In Python 2, the use of from __future__ import unicode_literals helps.

Update: how can the code be fixed:

OK – in your variable “source” you have some bytes. It is not clear from your question how they got in there – maybe you read them from a web form? In any case, they are not encoded with ascii, but python is trying to convert them to unicode assuming that they are. You need to explicitly tell it what the encoding is. This means that you need to know what the encoding is! That is not always easy, and it depends entirely on where this string came from. You could experiment with some common encodings – for example UTF-8. You tell unicode() the encoding as a second parameter:

source = unicode(source, 'utf-8')

Solution 2


Finally I got it:

as3:/usr/local/lib/python2.7/site-packages# cat sitecustomize.py
# encoding=utf8  
import sys  

reload(sys)  
sys.setdefaultencoding('utf8')

Let me check:

as3:~/ngokevin-site# python
Python 2.7.6 (default, Dec  6 2013, 14:49:02)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.getdefaultencoding()
'utf8'
>>>

The above shows the default encoding of python is utf8. Then the error is no more.

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