python 字符编码问题:u'\xb2\xe2\xca\xd4',类型为unicode,实际编码为utf-8,如何读取? - 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 Guid
Code Style from The Hitchhiker's Guide
yingluck

python 字符编码问题:u'\xb2\xe2\xca\xd4',类型为unicode,实际编码为utf-8,如何读取?

  •  
  •   yingluck
    zhao-ji Dec 7, 2013 14480 views
    This topic created in 4538 days ago, the information mentioned may be changed or developed.
    Supplement 1    Dec 7, 2013
    题目上的串错了,应该是这样的 u'\xe5\xbe\xae\xe4\xbf\xae'
    13 replies    2016-08-11 14:46:24 +08:00
    ushuz
        1
    ushuz  
       Dec 7, 2013 via iPhone   1
    转成str
    str()
    yingluck
        2
    yingluck  
    OP
       Dec 7, 2013
    @ushuz 转成了 但是a[3:-1]这样取出来还是读取不了啊
    Hackathon
        3
    Hackathon  
       Dec 7, 2013   3
    Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.
    >>> a = u'\xb2\xe2\xca\xd4'
    >>> b = a.encode('raw_unicode_escape')
    >>> print b
    测试
    >>> c = a.encode('latin1')
    >>> print c
    测试
    >>>
    yingluck
        4
    yingluck  
    OP
       Dec 7, 2013
    @Hackathon 行啊 哈哈哈哈 可以了! 多谢 我要把你这种方法好好学一下!
    sandtears
      &nbs; 5
    sandtears  
       Dec 7, 2013
    @Hackathon 多谢,我也一直很想知道这个orz
    lnehe
        6
    lnehe  
       Dec 7, 2013
    python的字符编码问题一直搞不懂。。。
    F0ur
        7
    F0ur  
       Dec 7, 2013
    python的字符编码一直个要研究的问题- -
    9hills
        8
    9hills  
       Dec 7, 2013
    这个不是字符编码问题<_<
    VYSE
        9
    VYSE  
       Dec 7, 2013
    标题里的就是'\xb2\xe2\xca\xd4'就是编码的,加u在encode转换其实蛮诡异的,不过latin1还能encode说明Python根据OS环境做了些取舍,放在英文默认编码系统里肯定转不出来。

    附言里是utf-8的,
    print '\xe5\xbe\xae\xe4\xbf\xae'.decode('utf-8')即可。

    \x出现在u''里表示的就不是byte而是等效于\u00XX,
    比如u'\xe5\xbe\xae\xe4\xbf\xae'其实等于u'\u00e5\u00be\u00ae\u00e4\u00bf\u00ae',这样表示的是unicode char table里的第XX位而不是字节,意义就全变了。

    反正bytes出现在unicode str里实在诡异。
    shenGun
        10
    shenGun  
       Dec 11, 2013
    http://docs.python.org/2/howto/unicode.html
    Latin-1, also known as ISO-8859-1, is a similar encoding. Unicode code points 0-255 are identical to the Latin-1 values, so converting to this encoding simply requires converting code points to byte values; if a code point larger than 255 is encountered, the string can’t be encoded into Latin-1.

    在Documentation中提示unicode的0-255编码和Latin-1的0-255是一样的。说以u'\xb2\xe2\xca\xd4'.decode('Latin-1')转好之后就是'\xb2\xe2\xca\xd4'其实好像还是的编码
    borneo
        11
    borneo  
       Dec 15, 2013
    hey man.

    by the way, keep it compatible with Python 2+3. http://lucumr.pocoo.org/2011/1/22/forwards-compatible-python/
    yingluck
        12
    yingluck  
    OP
       Aug 22, 2014
    http://hgoldfish.com/blogs/article/56/
    直到发现了这个资料
    About     Help     Advertise     Blog     API     FAQ     Solana     1083 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 46ms UTC 23:00 PVG 07:00 LAX 16:00 JFK 19:00
    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