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

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

sample code:

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


        
12条回答
  •  情歌与酒
    2020-11-21 23:59

    UPDATE: This is wrong answer, but it's still useful to understand why it's wrong. See comments.

    How about unicode-escape?

    >>> d = {1: "ברי צקלה", 2: u"ברי צקלה"}
    >>> json_str = json.dumps(d).decode('unicode-escape').encode('utf8')
    >>> print json_str
    {"1": "ברי צקלה", "2": "ברי צקלה"}
    

提交回复
热议问题