使用中文在Python中构建字典

Jer*_*opp 5 python unicode dictionary utf-8 cjk

所以这是我第一次来到这里,也是我对Python世界的新手.我也在学习中文,我想创建一个用字典来复习中文词汇的程序.这是我通常使用的代码:

#!/usr/bin/python
# -*- coding:utf-8-*-

dictionary = {"Hello" : "??"} # Simple example to save time

print(dictionary)
Run Code Online (Sandbox Code Playgroud)

我一直得到的结果是这样的:

{'hello': '\xe4\xbd\xa0\xe5\xa5\xbd'}
Run Code Online (Sandbox Code Playgroud)

我也尝试在字符串的开头添加一个"u"和中文字符,甚至是方法".encode('utf-8'),但这些似乎没有用.我通常在Geany工作IDE.我试图检查所有的偏好,我已经阅读了PEP网页以及发布的许多其他问题.这很有趣,它适用于字符串和raw_input方法,但没有别的......

unu*_*tbu 8

当打印字典时(例如print(dictionary)),repr显示键和值的s.

相反,尝试:

dictionary = {u"Hello" : u"??"} 
for key,value in dictionary.iteritems():
    print(u'{k} --> {v}'.format(k=key,v=value))
Run Code Online (Sandbox Code Playgroud)

收益率:

Hello --> ??
Run Code Online (Sandbox Code Playgroud)