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
88
V2EX  ›  Python

Python unicode 怎么显示中文?

  •  
  •   88 · 2014-09-07 11:27:55 +08:00 · 4963 次点击
    这是一个创建于 3516 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在通过HTTP得到一个字符串,返回的字符串像这样: data = "\u53f2\u5b89\u7fce"

    我想将它打印成中文: print data.encode('utf-8')
    可是显示还是这个字符串,求解。

    另外,该怎样将这个字符串插入MySQL。我的意思是,需不需要先将data中"\"进行转义?
    9 条回复    2016-01-20 20:14:24 +08:00
    hadoop
        1
    hadoop  
       2014-09-07 11:36:07 +08:00   ❤️ 6
    print data.decode("unicode_escape")
    88
        2
    88  
    OP
       2014-09-07 11:42:14 +08:00
    @hadoop 非常感谢!
    gamexg
        3
    gamexg  
       2014-09-07 13:33:16 +08:00
    原来还有 unicode_escape 这个编码,长见识了。
    totoismi
        4
    totoismi  
       2014-09-08 19:19:32 +08:00
    楼主这样的字符串data,用
    data.decode('unicode_escape')
    没问题。但是如果字符串中本身有反斜杠'\',那么decode('unicode_escape')会报错,所以最好使用
    data.decode('raw_unicode_escape')
    julyclyde
        5
    julyclyde  
       2014-09-08 22:58:51 +08:00 via iPad
    ohyeah521
        6
    ohyeah521  
       2014-09-11 15:16:15 +08:00
    学习了
    wshmelo
        7
    wshmelo  
       2014-09-16 13:23:35 +08:00
    @hadoop 学习了
    fucker
        8
    fucker  
       2014-09-16 23:59:23 +08:00
    mark
    yezhiye
        9
    yezhiye  
       2016-01-20 20:14:24 +08:00
    感谢,新手差点疯了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3302 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 13:18 · PVG 21:18 · LAX 06:18 · JFK 09:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.