why python json.dumps complains about ascii decoding? -


i have following lines in code

outs = codecs.getwriter('utf-8')(sys.stdout) # djson contains json message non-ascii chars outs.write(json.dumps(djson,encoding='utf-8', ensure_ascii=false, indent=indent_val)) 

i getting following exception:

    outs.write(json.dumps(djson,encoding='utf-8', ensure_ascii=false, indent=indent_val))     file "/usr/lib/python2.7/json/__init__.py", line 238, in dumps          **kw).encode(obj)     file "/usr/lib/python2.7/json/encoder.py", line 204, in encode          return ''.join(chunks)     unicodedecodeerror: 'ascii' codec can't decode byte 0xc3 in position 27: ordinal not in range(128) 

i through specifying encoding='utf-8' in json.dumps statement, avoid type of problem. why still getting error?

my guess djson object not contain pure unicode contains mix of unicode , strings encoded utf-8 e.g. fails

>>> d = {u'name':u'पाइथन'.encode('utf-8')} >>> json.dumps(d, encoding='utf-8', ensure_ascii=false) traceback (most recent call last):   file "<stdin>", line 1, in <module>   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/__init__.py", line 238, in dumps     **kw).encode(obj)   file "/system/library/frameworks/python.framework/versions/2.7/lib/python2.7/json/encoder.py", line 204, in encode     return ''.join(chunks) unicodedecodeerror: 'ascii' codec can't decode byte 0xe0 in position 1: ordinal not in range(128) 

but works (everything unicode)

>>> d = {u'name':u'पाइथन'} >>> json.dumps(d, encoding='utf-8', ensure_ascii=false) u'{"name": "\u092a\u093e\u0907\u0925\u0928"} 

though works (everything string)

>>> d = {'name':u'पाइथन'.encode('utf-8')} >>> json.dumps(d, encoding='utf-8', ensure_ascii=false) '{"name": "\xe0\xa4\xaa\xe0\xa4\xbe\xe0\xa4\x87\xe0\xa4\xa5\xe0\xa4\xa8"}' 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

c++ - Clear the memory after returning a vector in a function -

erlang - Saving a digraph to mnesia is hindered because of its side-effects -