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

Python 如何解码这段字符串为正常显示?

  •  
  •   zungmou · 2016-07-19 15:59:45 +08:00 · 5142 次点击
    这是一个创建于 2844 天前的主题,其中的信息可能已经有所发展或是发生改变。
    u'\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'

    如上,请高手赐教?
    12 条回复    2016-07-20 13:40:25 +08:00
    felixzhu
        1
    felixzhu  
       2016-07-19 16:09:54 +08:00

    print ''.join([chr(ord(i)) for i in u'\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'])
    jerryrong
        2
    jerryrong  
       2016-07-19 16:10:44 +08:00
    财务 /金融 /保险?
    lowzoom
        3
    lowzoom  
       2016-07-19 16:18:53 +08:00
    >>> print('\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'.decode('utf-8'))
    财务 /金融 /保险
    lightning1141
        5
    lightning1141  
       2016-07-19 16:30:56 +08:00
    a = u'\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'
    print a.encode('latin1').decode('utf8')

    http://stackoverflow.com/questions/9845842/bytes-in-a-unicode-python-string

    我想你应该解决这段编码怎么来的问题 :D
    changshu
        6
    changshu  
       2016-07-19 16:32:26 +08:00
    楼主的意思是为什么输入的是 u'unicode 数据'会变成 u'unicode 数据的 ascii 码', windows 下用 code.interact 打开的 shell 会有这问题, 貌似和默认编码有关
    zungmou
        7
    zungmou  
    OP
       2016-07-19 16:34:45 +08:00
    @yannxia

    感谢!

    x = u'\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'
    y = '\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'

    为什么这两种方式,用 print 打印 x 无法正常显示呢?如果我想正常显示 x ,应该怎么操作呢?
    zungmou
        8
    zungmou  
    OP
       2016-07-19 16:36:07 +08:00
    @lightning1141 十分感谢!您是怎么知道这段字符串是用 latin1 编码的呢?
    lightning1141
        9
    lightning1141  
       2016-07-19 16:45:25 +08:00
    @zungmou
    latin1 只是为了转换,利用了他单字节的特性,具体你可以查查资料。
    Magic347
        10
    Magic347  
       2016-07-19 17:18:21 +08:00   ❤️ 1
    http://stackoverflow.com/questions/14539807/convert-unicode-with-utf-8-string-as-content-to-str
    解这个问题的 tricky 之处在于利用这个特性:
    Unicode codepoints U+0000 to U+00FF all map one-on-one with the latin-1 encoding

    先将 unicode 字符串编码为 latin1 字符串,编码后保留了等价的字节流数据。
    而此时在这个问题中,这一字节流数据又恰恰对应了 utf8 编码,因此对其进行 utf8 解码即可还原最初的 unicode 字符。
    不过值得注意的是,需要确定的是形如\xe8\xb4\xa2 究竟是 utf8 编码还是类似 gbk 的其他类型编码,
    这一点对于最终正确还原 unicode 字符也是同样重要的。


    >>> x = u'\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'

    >>> x.encode("latin1")
    '\xe8\xb4\xa2\xe5\x8a\xa1/\xe9\x87\x91\xe8\x9e\x8d/\xe4\xbf\x9d\xe9\x99\xa9'

    >>> x.encode("latin1").decode("utf8")
    u'\u8d22\u52a1/\u91d1\u878d/\u4fdd\u9669'

    >>> print x.encode("latin1").decode("utf8")
    财务 /金融 /保险
    zungmou
        11
    zungmou  
    OP
       2016-07-19 23:12:09 +08:00
    @Magic347 感谢!感觉 python 在编码处理上稍微复杂,掌握不好容易出问题,但是掌握好了又比其它语言灵活!
    lilydjwg
        12
    lilydjwg  
       2016-07-20 13:40:25 +08:00
    @zungmou 任何语言处理你这个编码错了的字符串都比较复杂。 Python 2 因为自动转码导致比较难以理解, Python 3 显式区分已编码数据和未编码的 Unicode 字符串,更容易理解。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   759 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:48 · PVG 05:48 · LAX 14:48 · JFK 17:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.