简体   繁体   中英

Python 'utf8' codec can't decode byte 0xcd in position 0: invalid continuation byte

I'm trying to convert the below 'dictionary'( x ) to 'JSON'. But i encounterd the following data is not JSON serializable.

STEP : 1

>>> import json
>>> import datetime
>>> x={'status': 1, 'MetaInformation': {'create_time': datetime.datetime(2015, 6, 17, 7, 43),'last_saved_by': '\xcd\xf2\xbb\xa7\xcd\xf8\xc2\xe7','author': '\xcd\xf2\xbb\xa7\xcd\xf8\xc2\xe7','last_printed': datetime.datetime(2015, 6, 10, 6, 54), 'last_saved_time': datetime.datetime(2015, 6, 18, 0, 9)}}
>>> json_data=json.dumps(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2015, 6, 10, 6, 54) is not JSON serializable

STEP 2:

I removed few fields from the dictionary to debug the issue,

x={'status': 1, 'MetaInformation': {'create_time': datetime.datetime(2015, 6, 17, 7, 43),'last_printed': datetime.datetime(2015, 6, 10, 6, 54), 'last_saved_time': datetime.datetime(2015, 6, 18, 0, 9)}}

SOLUTION :

>>> from bson import json_util
>>> import json
>>> import datetime
>>> x={'status': 1, 'MetaInformation': {'create_time': datetime.datetime(2015, 6, 17, 7, 43),'last_printed': datetime.datetime(2015, 6, 10, 6, 54), 'last_saved_time': datetime.datetime(2015, 6, 18, 0, 9)}}
>>> y=json.dumps(x, default=json_util.default)
>>> print y
{"status": 1, "MetaInformation": {"create_time": {"$date": 1434526980000}, "last_saved_time": {"$date": 1434586140000}, "last_printed": {"$date": 1433919240000}}}

But when i included the removed data ( STEP 2) alone to convert the dictionary to JSON array , I do get the following error

>>> y=json.dumps(x, default=json_util.default)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/json/__init__.py", line 251, in dumps
    sort_keys=sort_keys, **kw).encode(obj)
  File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcd in position 0: invalid continuation byte

I'm a newbie in python. Any help on converting the dictionary ( x ) above to JSON array will be helpful ?

datetime.datetime doesn't return a string, so it cannot be processed by json.dumps . To change it use str(datetime.datetime(2015, 6, 17, 7, 43))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM