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

setdefaultencoding 后无法 print

  •  
  •   oldbird · 2019-04-16 15:23:49 +08:00 · 1215 次点击
    这是一个创建于 1834 天前的主题,其中的信息可能已经有所发展或是发生改变。

    import sys

    reload(sys)

    sys.setdefaultencoding('utf-8')

    for t in range(100):

    print t
    

    py2.7 中,上述代码在 IDE 里无法打印出值,通过 cmd 运行正常; 注释掉前三句后,IDE 和 CMD 运行都正常。 不知道为什么?

    4 条回复    2019-04-16 16:02:14 +08:00
    soulzz
        1
    soulzz  
       2019-04-16 15:38:07 +08:00
    python 中编码确实让人很头疼
    你代码顺序错了
    import sys
    sys.setdefaultencoding('utf-8')
    reload(sys)
    for t in range(100):
    print t
    ysc3839
        2
    ysc3839  
       2019-04-16 15:41:38 +08:00
    “无法打印出值”是怎么个无法?
    shuax
        3
    shuax  
       2019-04-16 15:41:53 +08:00
    是时候用 Python3 了
    oldbird
        4
    oldbird  
    OP
       2019-04-16 16:02:14 +08:00
    @shuax 主要是宿主软件必须用 py2.....
    找到解决方案了
    https://www.cnblogs.com/huangbiquan/p/7812005.html
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5057 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.