Chinaunix

标题: 如下如何在dict中显示中文? [打印本页]

作者: fu7100    时间: 2018-10-20 18:46
标题: 如下如何在dict中显示中文?
a=str({"a":"中国","b":"美国"})
b=str('中国')

print a.encode('gbk')
print b.encode('gbk') 执行结果如下: {'a': '\xe4\xb8\xad\xe5\x9b\xbd', 'b': '\xe7\xbe\x8e\xe5\x9b\xbd'}
中国

作者: dahe_1984    时间: 2018-10-24 18:59
python3
Python 3.7.0 (default, Oct  2 2018, 09:20:07)
[Clang 10.0.0 (clang-1000.11.45.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a={"a":"中国","b":"美国"}
>>> a['a']
'中国'
>>> a
{'a': '中国', 'b': '美国'}
>>>
用py3, 不用考虑这些烂马起遭的问题
作者: fu7100    时间: 2018-10-24 20:12
回复 2# dahe_1984

有些包只有py2有无法切换到py3呢

作者: dahe_1984    时间: 2018-11-05 09:49
>>> import json
>>> a={"a":"中国","b":"美国"}
>>> print json.dumps(a, ensure_ascii=False, encoding='UTF-8')
{"a": "中国", "b": "美国"}





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2