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

前端 未结 12 1185
说谎
说谎 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:17

    use unicode-escape to solve problem

    >>>import json
    >>>json_string = json.dumps("ברי צקלה")
    >>>json_string.encode('ascii').decode('unicode-escape')
    '"ברי צקלה"'
    

    explain

    >>>s = '漢  χαν  хан'
    >>>print('unicode: ' + s.encode('unicode-escape').decode('utf-8'))
    unicode: \u6f22  \u03c7\u03b1\u03bd  \u0445\u0430\u043d
    
    >>>u = s.encode('unicode-escape').decode('utf-8')
    >>>print('original: ' + u.encode("utf-8").decode('unicode-escape'))
    original: 漢  χαν  хан
    

    original resource:https://blog.csdn.net/chuatony/article/details/72628868

提交回复
热议问题