How do I tell dict() in Python 2 to use unicode instead of byte string?

為{幸葍}努か 提交于 2019-12-04 07:48:33

To get a dict with Unicode keys, use Unicode strings when constructing the dict:

>>> d = {u'a': 2}
>>> d
{u'a': 2}

Dicts created from keyword arguments always have string keys. If you want those to be Unicode (as well as all other strings), switch to Python 3.

Keyword arguments in 2.x can only use ASCII characters, which means bytestrings. Either use a dict literal, or use one of the constructors that allows specifying the full type.

>>> dict(((u'a', 2),))
{u'a': 2}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!