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-02-20 05:33:29 +08:00 · 7244 次点击
    这是一个创建于 3717 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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-02-20 07:38:13 +08:00
    目的是,print出来。
    现状是print提示错误UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range
    第 2 条附言  ·  2014-02-20 12:34:58 +08:00
    已解决,Pycharm的终端没有中文支持。
    9 条回复    1970-01-01 08:00:00 +08:00
    xhacker
        1
    xhacker  
       2014-02-20 06:57:02 +08:00   ❤️ 1
    你到底想获得什么?rs 现在已经是一个 unicode 字符串了啊。
    kchum
        2
    kchum  
    OP
       2014-02-20 07:30:06 +08:00
    @xhacker 我是希望能print出来,现在print的时候会提示UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2
    pt
        3
    pt  
       2014-02-20 07:47:47 +08:00   ❤️ 1
    直接:
    print(rs.encode('utf-8'))

    参考:http://stackoverflow.com/a/492711/114833
    tempdban
        4
    tempdban  
       2014-02-20 07:48:32 +08:00 via Android   ❤️ 1
    直接print 另外ide的console基本没有中文支持
    geew
        5
    geew  
       2014-02-20 09:11:43 +08:00   ❤️ 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-02-20 12:33:57 +08:00
    @tempdban
    @geew
    @pt
    @xhacker
    感谢各位关注,正解如4楼所说
    折腾半天,原来是Pycharm的终端没有中文支持。
    exoticknight
        7
    exoticknight  
       2014-02-20 13:02:30 +08:00
    C:\Python27\python.exe Z:/python/pyqttest/main.py
    中文哦

    Process finished with exit code 0

    怎么我能print得出来……
    kchum
        8
    kchum  
    OP
       2014-02-20 14:48:46 +08:00
    @exoticknight 是Pycharm的Terminal不支持unicode吧,我是在IDE里面调试的。用iTerm则没有出错
    exoticknight
        9
    exoticknight  
       2014-02-20 17:54:16 +08:00
    @kchum
    非要我截图吗,贴图好麻烦……
    http://www.image-share.com/ijpg-2444-111.html
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5402 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 07:39 · PVG 15:39 · LAX 00:39 · JFK 03:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.