V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
noobsheldon
V2EX  ›  Python

Python2 的 exec 语句中 Unicode 和 正常 Unicode 的问题? 这个两个输出结果的区别是什么原因造成的?

  •  
  •   noobsheldon · Aug 19, 2017 · 1542 views
    This topic created in 3172 days ago, the information mentioned may be changed or developed.
    # exec 语句中
    exec "res=u'中国'"
    type(res)   # <type 'unicode'>
    res         # u'\xe4\xb8\xad\xe5\x9b\xbd'
    
    # 正常 Unicode
    v=u'中国'
    type(v)     # <type 'unicode'>
    v           # u'\u4e2d\u56fd'
    
    
    2 replies    2017-08-19 17:48:39 +08:00
    ltux
        1
    ltux  
       Aug 19, 2017 via Android   ❤️ 1
    你的写法造成的。
    exec 把 "res=u'中国'" 当成一个 latin-1 编码的 str 来执行了呗。
    est
        2
    est  
       Aug 19, 2017
    试试 exec u"res=u'中国'" ?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5238 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 01:16 · PVG 09:16 · LAX 18:16 · JFK 21:16
    ♥ Do have faith in what you're doing.