V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Sep 7, 2014 · 6008 views
    This topic created in 4251 days ago, the information mentioned may be changed or developed.
    现在通过HTTP得到一个字符串,返回的字符串像这样: data = "\u53f2\u5b89\u7fce"

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

    另外,该怎样将这个字符串插入MySQL。我的意思是,需不需要先将data中"\"进行转义?
    9 replies    2016-01-20 20:14:24 +08:00
    hadoop
        1
    hadoop  
       Sep 7, 2014   ❤️ 6
    print data.decode("unicode_escape")
    88
        2
    88  
    OP
       Sep 7, 2014
    @hadoop 非常感谢!
    gamexg
        3
    gamexg  
       Sep 7, 2014
    原来还有 unicode_escape 这个编码,长见识了。
    totoismi
        4
    totoismi  
       Sep 8, 2014
    楼主这样的字符串data,用
    data.decode('unicode_escape')
    没问题。但是如果字符串中本身有反斜杠'\',那么decode('unicode_escape')会报错,所以最好使用
    data.decode('raw_unicode_escape')
    julyclyde
        5
    julyclyde  
       Sep 8, 2014 via iPad
    ohyeah521
        6
    ohyeah521  
       Sep 11, 2014
    学习了
    wshmelo
        7
    wshmelo  
       Sep 16, 2014
    @hadoop 学习了
    fucker
        8
    fucker  
       Sep 16, 2014
    mark
    yezhiye
        9
    yezhiye  
       Jan 20, 2016
    感谢,新手差点疯了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1442 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 16:55 · PVG 00:55 · LAX 09:55 · JFK 12:55
    ♥ Do have faith in what you're doing.