请教下 python 的 dict,有 unicode 该如何解决 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
kchum
V2EX    Python

请教下 python 的 dict,有 unicode 该如何解决

  •  
  •   kchum 2014 年 2 月 20 日 8208 次点击
    这是一个创建于 4435 天前的主题,其中的信息可能已经有所发展或是发生改变。
    python不太熟
    代码如下,我想取得dst对应的值:音乐(\u97f3\u4e50)

    t={u'src': u'[ Music ]', u'dst': u'\u97f3\u4e50'}
    rs=t.get('dst')
    print(rs)

    print(rs.decode('utf-8').encode('ascii'))

    用过decode,encode...
    提示UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)

    网上查过试过
    reload(sys)
    sys.setdefaultencoding( "utf-8" )

    没效果,测试过,就是print的时候才提示错误的。。试了2个小时,唉。

    环境python 2.7.5
    第 1 条附言    2014 年 2 月 20 日
    目的是,print出来。
    现状是print提示错误UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range
    第 2 条附言    2014 年 2 月 20 日
    已解决,Pycharm的终端没有中文支持。
    9 条回复    1970-01-01 08:00:00 +08:00
    xhacker
        1
    xhacker  
       2014 年 2 月 20 日   1
    你到底想获得什么?rs 现在已经是一个 unicode 字符串了啊。
    kchum
        2
    kchum  
    OP
       2014 年 2 月 20 日
    @xhacker 我是希望能print出来,现在print的时候会提示UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2
    pt
        3
    pt  
       2014 年 2 月 20 日   1
    直接:
    print(rs.encode('utf-8'))

    参考:http://stackoverflow.com/a/492711/114833
    tempdban
        4
    tempdban  
       2014 年 2 月 20 日 via Android   1
    直接print 另外ide的console基本没有中文支持
    geew
        5
    geew  
       2014 年 2 月 20 日   1
    In [1]: t={u'src': u'[ Music ]', u'dst': u'\u97f3\u4e50'}

    In [2]: rs=t.get('dst')

    In [3]: print(rs) # 直接print就行了啊
    音乐

    In [4]: print(rs.decode('utf-8').encode('ascii')) # 中文encode ascii肯定会报这个错
    ---------------------------------------------------------------------------
    UnicodeEncodeError Traceback (most recent call last)
    /home/geew/familyparty/<ipython-input-4-72fd90a6715b> in <module>()
    ----> 1 print(rs.decode('utf-8').encode('ascii'))
    kchum
        6
    kchum  
    OP
       2014 年 2 月 20 日
    @tempdban
    @geew
    @pt
    @xhacker
    感谢各位关注,正解如4楼所说
    折腾半天,原来是Pycharm的终端没有中文支持。
    exoticknight
        7
    exoticknight  
       2014 年 2 月 20 日
    C:\Python27\python.exe Z:/python/pyqttest/main.py
    中文哦

    Process finished with exit code 0

    怎么我能print得出来……
    kchum
        8
    kchum  
    OP
       2014 年 2 月 20 日
    @exoticknight 是Pycharm的Terminal不支持unicode吧,我是在IDE里面调试的。用iTerm则没有出错
    exoticknight
        9
    exoticknight  
       2014 年 2 月 20 日
    @kchum
    非要我截图吗,贴图好麻烦……
    http://www.image-share.com/ijpg-2444-111.html
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     956 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 29ms UTC 20:20 PVG 04:20 LAX 13:20 JFK 16:20
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86