Skip to content
Advertisement

UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0xa5 in position 0: invalid start byte

I am using Python-2.6 CGI scripts but found this error in server log while doing json.dumps(),

JavaScript

​Here ,

​__get​data() function returns dictionary {} .

Before posting this question I have referred this of question os SO.


UPDATES

Following line is hurting JSON encoder,

JavaScript

I got a temporary fix for it

JavaScript

But I am not sure is it correct way to do it.

Advertisement

Answer

The error is because there is some non-ascii character in the dictionary and it can’t be encoded/decoded. One simple way to avoid this error is to encode such strings with encode() function as follows (if a is the string with non-ascii character):

JavaScript
Advertisement