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

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

    To write to a file

    import codecs
    import json
    
    with codecs.open('your_file.txt', 'w', encoding='utf-8') as f:
        json.dump({"message":"xin chào việt nam"}, f, ensure_ascii=False)
    

    To print to stdout

    import json
    print(json.dumps({"message":"xin chào việt nam"}, ensure_ascii=False))
    

提交回复
热议问题