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

加了 io.TextIOWrapper 处理后, print()内容就跟不上执行节奏了。。。

  •  
  •   pppguest3962 · 2020-02-13 22:00:39 +08:00 · 2442 次点击
    这是一个创建于 1532 天前的主题,其中的信息可能已经有所发展或是发生改变。

    最近,从一些来源(如网页,MySQL)取得字符串后,print 会报错,但事实上字符只是带了一些东欧波兰语的字符,print()就无法处理了。。。
    根据网上的建议,加入:

    import sys, io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
    

    然后,print 就能正常输出字符串了
    然而
    在大量处理(输出结果)的时候, 这些 print,会缓积到几百行的时候,才一下子在屏幕上显示出来,
    去掉上面两句,print 输出节奏,又像是以前一样了 这样对调试程序很不便, 如何配置 pycharm 或者修改程序,才能两种情况都能兼顾呢?

    1 条回复    2020-02-15 11:11:29 +08:00
    frostming
        1
    frostming  
       2020-02-15 11:11:29 +08:00
    print(xxx, flush=True)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5425 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:23 · PVG 15:23 · LAX 00:23 · JFK 03:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.