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()
,
Traceback (most recent call last):
File "/etc/mongodb/server/cgi-bin/getstats.py", line 135, in <module>
print json.dumps(__getdata())
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
Here ,
__getdata()
function returns dictionary {}
.
Before posting this question I have referred this of question os SO.
UPDATES
Following line is hurting JSON encoder,
now = datetime.datetime.now()
now = datetime.datetime.strftime(now, '%Y-%m-%dT%H:%M:%S.%fZ')
print json.dumps({'current_time': now}) # this is the culprit
I got a temporary fix for it
print json.dumps( {'old_time': now.encode('ISO-8859-1').strip() })
But I am not sure is it correct way to do it.
I switched this simply by defining a different codec package in the read_csv()
command:
encoding = 'unicode_escape'
Eg:
import pandas as pd
data = pd.read_csv(filename, encoding= 'unicode_escape')
Try the below code snippet:
with open(path, 'rb') as f:
text = f.read()