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

Python ascii()函数的逆操作

  •  
  •   MiketsuSmasher · 2021-09-18 22:24:08 +08:00 · 2051 次点击
    这是一个创建于 922 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> ascii('泷夜叉姬')
    "'\\u6cf7\\u591c\\u53c9\\u59ec'"
    

    如何将"'\\u6cf7\\u591c\\u53c9\\u59ec'"还原回去?

    5 条回复    2021-09-22 09:50:09 +08:00
    matrixji
        1
    matrixji  
       2021-09-18 22:36:49 +08:00
    >>> eval(ascii('泷夜叉姬'))
    '泷夜叉姬'
    >>>
    getcharch
        2
    getcharch  
       2021-09-18 22:41:59 +08:00
    ascii('泷夜叉姬').encode('ascii').decode('unicode_escape')
    iBugOne
        3
    iBugOne  
       2021-09-18 23:19:14 +08:00 via Android   ❤️ 1
    >>> import ast
    >>> ast.literal_eval(ascii('泷夜叉姬'))
    '泷夜叉姬'
    >>>
    yuguorui96
        4
    yuguorui96  
       2021-09-19 17:03:15 +08:00
    frostming
        5
    frostming  
       2021-09-22 09:50:09 +08:00
    #2 是「正」解,literal_eval 是次优解,eval 是歪解
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3194 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 14:25 · PVG 22:25 · LAX 07:25 · JFK 10:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.