推荐学习书目
› 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 · 6568 views
    This topic created in 4451 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
    hadoop
    Sep 7, 2014❤️6
    1
    print data.decode("unicode_escape")
    88
    88
    OP
    Sep 7, 2014
    2
    @hadoop 非常感谢!
    gamexg
    gamexg
    Sep 7, 2014
    3
    原来还有 unicode_escape 这个编码,长见识了。
    totoismi
    totoismi
    Sep 8, 2014
    4
    楼主这样的字符串data,用
    data.decode('unicode_escape')
    没问题。但是如果字符串中本身有反斜杠'\',那么decode('unicode_escape')会报错,所以最好使用
    data.decode('raw_unicode_escape')
    julyclyde
    julyclyde
    Sep 8, 2014 via iPad
    5
    赞
    ohyeah521
    ohyeah521
    Sep 11, 2014
    6
    学习了
    wshmelo
    wshmelo
    Sep 16, 2014
    7
    @hadoop 学习了
    fucker
    fucker
    Sep 16, 2014
    8
    mark
    yezhiye
    yezhiye
    Jan 20, 2016
    9
    感谢,新手差点疯了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1101 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:42 · PVG 07:42 · LAX 15:42 · JFK 18:42
    ♥ Do have faith in what you're doing.