Make utf8 readable in a file

后端 未结 2 1400
情歌与酒
情歌与酒 2021-01-20 03:36

I have dictionary of dictionary which has utf8 encoded keys. I am dumping this dictionary to a file using json module.
In the file keys are printed in utf8

2条回答
  •  不要未来只要你来
    2021-01-20 04:27

    use ensure_ascii parameter.

    >>> import json
    >>> print json.dumps(u'\u0982')
    "\u0982"
    >>> print json.dumps(u'\u0982', ensure_ascii=False)
    "ং"
    

    http://docs.python.org/2/library/json.html#json.dump

    If ensure_ascii is True (the default), all non-ASCII characters in the output are escaped with \uXXXX sequences, and the result is a str instance consisting of ASCII characters only. If ensure_ascii is False, some chunks written to fp may be unicode instances. ...

提交回复
热议问题