Programmers learn & share
0 votes
664 views

Problem :

I am new to the Python, I am using Python-2.6 CGI scripts but facing following error in the server log while I was doing json.dumps(),

Traceback (most recent call last):
File "/etc/mongodb/server/cgi-bin/getstats.py", line 135, in <module>
print json.dumps(​​__get​data())
File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 201, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode
return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa5 in position 0: invalid start byte

​Please Note :

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

I guess the following lines are causing JSON encoder error

now = datetime.datetime.now()
now = datetime.datetime.strftime(now, '%Y-%m-%dT%H:%M:%S.%fZ')
print json.dumps({'current_time': now}) // I guess this is the culprit
by (7.5k points)   | 664 views

1 Answer

0 votes

Solution :

I guess your string has a non ascii characters encoded in it.

I unable to decode with utf-8 it may happen if you need to use other encodings in your code. For e.g.:

>>> 'my weird character \x96'.decode('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x96 in position 19: invalid start byte

In your case, the encoding seems to be windows-1252 so you need to do as follows:

>>> 'my weird character \x96'.decode('windows-1252')
u'my weird character \u2013'

Now you have unicode so you can safely encode into utf-8.

by (36.1k points)  
2,227 questions
2,734 answers
59 comments
241 users