Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence

前端 未结 12 1170
说谎
说谎 2020-11-21 23:25

sample code:

>>> import json
>>> json_string = json.dumps(\"ברי צקלה\")
>>> print json_string
\"\\u05d1\\u05e8\\u05d9 \\u05e6\\u05         


        
12条回答
  •  滥情空心
    2020-11-22 00:21

    Peters' python 2 workaround fails on an edge case:

    d = {u'keyword': u'bad credit  \xe7redit cards'}
    with io.open('filename', 'w', encoding='utf8') as json_file:
        data = json.dumps(d, ensure_ascii=False).decode('utf8')
        try:
            json_file.write(data)
        except TypeError:
            # Decode data to Unicode first
            json_file.write(data.decode('utf8'))
    
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 25: ordinal not in range(128)
    

    It was crashing on the .decode('utf8') part of line 3. I fixed the problem by making the program much simpler by avoiding that step as well as the special casing of ascii:

    with io.open('filename', 'w', encoding='utf8') as json_file:
      data = json.dumps(d, ensure_ascii=False, encoding='utf8')
      json_file.write(unicode(data))
    
    cat filename
    {"keyword": "bad credit  çredit cards"}
    

提交回复
热议问题