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

Python3 decode 问题

  •  
  •   scriptB0y ·
    laixintao · 2018-02-28 10:58:56 +08:00 · 3633 次点击
    这是一个创建于 2248 天前的主题,其中的信息可能已经有所发展或是发生改变。

    求解如果 str 混入了 bytes,但是被按照字面意思解释了,怎么 decode 回去。

    In [80]: original = "abc\\xe2\\x86\\x92"
    
    In [81]: b'\xe2\x86\x92'.decode()
    Out[81]: '→'
    
    In [82]: what_i_want = "abc→"
    
    In [83]: what_i_want.encode()
    Out[83]: b'abc\xe2\x86\x92'
    
    第 1 条附言  ·  2018-02-28 11:52:22 +08:00
    问题是:

    将 "abc\\xe2\\x86\\x92" ( str) 转换成 'abc→' (str) 或 b'abc\xe2\x86\x92' ( bytes)
    第 2 条附言  ·  2018-02-28 14:00:56 +08:00

    找到方法了:

    In [107]: original.encode().decode('unicode-escape').encode('latin1').decode('utf-8') 
    Out[107]: 'abc→' 
    
    13 条回复    2018-02-28 13:48:37 +08:00
    Monad
        1
    Monad  
       2018-02-28 11:28:36 +08:00
    exp = '"{}".decode("UTF-8")'.format(original)
    what_i_want = eval(exp)
    =.=
    scriptB0y
        2
    scriptB0y  
    OP
       2018-02-28 11:32:03 +08:00
    @Monad 不行吧。。。

    In [87]: eval( '"{}".decode("UTF-8")'.format(original) )
    -------------------------------------------------------------
    AttributeError Traceback (most recent call last)
    <ipython-input-87-9a50a9092a0f> in <module>()
    ----> 1 eval( '"{}".decode("UTF-8")'.format(original) )

    <string> in <module>()

    AttributeError: 'str' object has no attribute 'decode'
    Thanks
        3
    Thanks  
       2018-02-28 11:37:47 +08:00
    emmmm...

    首先,\\ 双反代表一个不具备转移功能的 \
    其次,b"abc\xe2\x86\x92" 才能用 decode()处理编码,注意这个是 bytes,里面是单 \
    最后,
    Python3.6 下有:
    >>> b = b"abc\xe2\x86\x92"
    >>> b.decode()
    'abc→'
    Thanks
        4
    Thanks  
       2018-02-28 11:38:16 +08:00
    转移 => 转义
    scriptB0y
        5
    scriptB0y  
    OP
       2018-02-28 11:50:32 +08:00
    @Thanks 你说的我都知道。我的问题是把 original = "abc\\xe2\\x86\\x92" 转换成 'abc→'
    Monad
        6
    Monad  
       2018-02-28 12:15:12 +08:00   ❤️ 1
    @scriptB0y #2 这是 Python2 的方式
    original.decode('string_escape')
    这个应该是 2/3 都可以的 你试试
    scriptB0y
        7
    scriptB0y  
    OP
       2018-02-28 12:20:05 +08:00
    @Monad str 没有 decode 方法啊。
    scriptB0y
        8
    scriptB0y  
    OP
       2018-02-28 12:31:50 +08:00
    @Monad 顺着你的思路找了解决方法了!

    In [107]: original.encode().decode('unicode-escape').encode('latin1').decode('utf-8')
    Out[107]: 'abc→'

    太好玩了
    Monad
        9
    Monad  
       2018-02-28 12:35:26 +08:00
    @scriptB0y #8 刚刚傻了 shebang 里面写的 python3 但是一直用 python xx.py 执行的...
    想不起来为啥要 encode('latin-1')了 求告知
    scriptB0y
        10
    scriptB0y  
    OP
       2018-02-28 12:43:10 +08:00   ❤️ 1
    @Monad decode('unicode-escape') 的结果是 latin-1 编码的,这是错误的,所以要 encode 回去然后使用正确的 utf-8 decode。

    这里文档有说: https://docs.python.org/3/library/codecs.html#python-specific-encodings (搜索 unicode_escape )
    Monad
        11
    Monad  
       2018-02-28 13:11:41 +08:00 via iPhone
    @scriptB0y 哦哦 应该是 decode("unicode-escape")做了两个工作 一个是 escape 一个是 decode 由于不能支持两个 decode 参数 所以必须 encode 成 latin1 再 decode

    decode 如果加一个 kwargs escape=True 那么就可以直接按 utf8 decode 了
    scriptB0y
        12
    scriptB0y  
    OP
       2018-02-28 13:16:30 +08:00
    @Monad 是的,我也是这么想的。
    zzhirong
        13
    zzhirong  
       2018-02-28 13:48:37 +08:00
    @scriptB0y
    @Monad
    python2 中可以,python3 中少了一个 'b'
    exp = 'b"{}".decode("UTF-8")'.format(original)
    what_i_want = eval(exp)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3263 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:09 · PVG 22:09 · LAX 07:09 · JFK 10:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.