python - annoying unicode error while dumping json -


so got error message:

traceback (most recent call last):   file "make.py", line 48, in <module>     json.dump(amazon_review, outfile)   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/__init__.py", line 189, in dump     chunk in iterable:   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py", line 434, in _iterencode     chunk in _iterencode_dict(o, _current_indent_level):   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py", line 408, in _iterencode_dict     chunk in chunks:   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py", line 313, in _iterencode_list     yield buf + _encoder(value) unicodedecodeerror: 'utf8' codec can't decode byte 0xea in position 173: invalid continuation byte 

on these code:

with open('amazon_review.json', 'w') outfile:   json.dump(amazon_review, outfile) 

i figure out. great.

python 2 doesn't use unicode interfaces though returns unicode strings, it'll never read non-ansi characters correctly.

so attempt .encode fails unicode​decode​error trying unicode string before encoding ascii.try using this.

with open('amazon_review.json', 'w') outfile:     try:         json.dump(amazon_review, outfile)# omit in 3.x!     except unicodeencodeerror:         pass 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -