Wednesday, 11 April 2018

Encode Decode error in python : UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0

Error desc:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0:
                    ordinal not in range(128)

This type of error comes when some special character is not in range of selected encoding type.

Solution :

Find the default encoding type of python.
             python -c 'import sys; print(sys.getdefaultencoding())'
             o/p : ascii

We need to change the encoding type and try with different type of encoding. e.g.

latin-1
utf-8
utf-16-be


Create a file sitecustomize.py under this location : /usr/lib/python2.7/site-packages/sitecustomize.py

echo "import sys; sys.setdefaultencoding('latin-1')" > sitecustomize.py


Compile the python script and check the encoding type is changed by below command.
python -m py_compile sitecustomize.py

PYTHONPATH=".:$PYTHONPATH" python -c 'import sys; print(sys.getdefaultencoding())'

o/p : latin-1

No comments:

Post a Comment

Thank You for your valuable comment

Difference between class level and object locking and static object lock

1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks. 2) Object locking will lo...