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

为什么程序在 Windows 系统中标准输出和重定向到文件后的输出结果不一样?

  •  
  •   C0VN · Dec 28, 2017 · 2763 views
    This topic created in 3042 days ago, the information mentioned may be changed or developed.

    p.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys, locale 
    print(sys.stdout.encoding, sys.getdefaultencoding(), locale.getpreferredencoding())
    print('你好,世界')
    

    python 版本 3.6.3,中文 Windows 7 系统,cmd 下执行以下命令

    C:\Users\>python p.py
    utf-8 utf-8 cp936
    你好,世界
    
    C:\Users\>python p.py > p.txt
    

    p.txt 编码为 ANSI,内容为

    cp936 utf-8 cp936
    你好,世界
    

    sys.stdout.encoding的结果不同,为什么呢?

    Supplement 1  ·  Dec 31, 2017
    [有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解] https://www.v2ex.com/t/163786
    有一定的帮助
    5 replies    2017-12-28 19:48:52 +08:00
    lniwn
        1
    lniwn  
       Dec 28, 2017
    我觉得 help(sys.stdout),然后再看看这篇文章<https://github.com/tartley/colorama/issues/125>或许对你有帮助。
    justou
        2
    justou  
       Dec 28, 2017
    这样说明是不是很容易理解?

    import sys, locale
    fh = open("f.txt", "w", encoding="utf-8") # 换成"gbk", "big5"等其它编码试试
    sys.stdout = fh
    print(sys.stdout.encoding, sys.getdefaultencoding(), locale.getpreferredencoding())
    print('你好,世界')
    fh.close()
    geelaw
        3
    geelaw  
       Dec 28, 2017 via iPhone
    因为打开一个文件默认是用 preferred encoding indicated by locale,而 console 是 UTF-8。
    C0VN
        4
    C0VN  
    OP
       Dec 28, 2017
    应该和 isatty() 有关。
    sys.stdout.isatty()在直接执行的情况下返回 True,如果输出重定向到文件则返回 False。
    文档中找到了相应的解释

    The character encoding is platform-dependent. Under Windows, if the stream is interactive (that is, if its isatty() method returns True), the console codepage is used, otherwise the ANSI code page. Under other platforms, the locale encoding is used (see locale.getpreferredencoding()).

    可能就如这篇文章中说的一样
    http://blog.csdn.net/haiross/article/details/36189103
    ```
    if sys.stdout.isatty():
    default_encoding = sys.stdout.encoding
    else:
    default_encoding = locale.getpreferredencoding()
    ```
    但是我还不清楚如何去源代码中具体查看。
    C0VN
        5
    C0VN  
    OP
       Dec 28, 2017
    可是文档里说`if the stream is interactive (that is, if its isatty() method returns True), the console codepage is used, otherwise the ANSI code page`
    chcp 执行后显示代码页为 936,为什么 sys.stdout.encoding 会是 utf-8 呢?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   952 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 326ms · UTC 21:15 · PVG 05:15 · LAX 14:15 · JFK 17:15
    ♥ Do have faith in what you're doing.