在Python中的Stdout编码 [英] Stdout encoding in python

查看:809
本文介绍了在Python中的Stdout编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有很好的理由,我不应该开始所有我的Python程序与此?在执行这样的exec时,有什么特别的丢失吗?

Is there a good reason why I shouldn't start all my python programs with this? Is there something special lost when doing exec like this?

#!/usr/bin/python
import os, sys
if sys.stdout.encoding == None:
    os.putenv("PYTHONIOENCODING",'UTF-8')
    os.execv(sys.executable,['python']+sys.argv)
print sys.stdout.encoding

有关PYTHONIOENCODING的问题,所以我想这是一个常见的问题,但是如果你不知道,这是因为当 sys.stdout.encoding == None 那么你只能打印ascii字符,所以例如 printåäö会抛出例外..

There are 60 questions about PYTHONIOENCODING so I guess it's a common problem, but in case you don't know, this is done because when sys.stdout.encoding == None then you can only print ascii chars, so e.g. print "åäö" will throw an exception..

EDIT 当stdout是管道时; python encoding.py | cat 会将编码设置为无

EDIT This happens to me when stdout is a pipe; python encoding.py|cat will set encoding to None

另一种解决方法是更改​​ stdout的codec sys.stdout = codecs.getwriter('utf8')(sys.stdout)这是我猜测是正确的答案解决的问题的意见。

Another solution is to change the codec of stdout sys.stdout = codecs.getwriter('utf8')(sys.stdout) which I'm guessing is the correct answer dispite the comments on that question.

推荐答案

是的,有一个很好的理由不要启动所有的Python程序。

Yes, there is a good reason not to start all your Python programs like that.

首先:

sys.stdout.encoding 为None。在大多数情况下,这是因为它根本不支持任何编码。在你的情况下,这是因为stdout是一个文件,而不是一个终端。但是它可以设置为None,因为Python也无法检测终端的编码。

sys.stdout.encoding is None if Python doesn't know what encoding the stdout supports. This, in most cases, is because it doesn't really support any encoding at all. In your case it's because the stdout is a file, and not a terminal. But it could be set to None because Python also fails to detect the encoding of the terminal.

其次:设置环境变量,然后启动一个新进程smae命令。这很丑陋。

Second of all: You set the environment variable and then start a new process with the smae command again. That's pretty ugly.

所以,除非你打算成为唯一一个使用你的程序,你不应该这样开始。但如果你计划是唯一使用你的程序,然后继续。

So, unless you plan to be the only one using your programs, you shouldn't start them like that. But if you do plan to be the only using your program, then go ahead.

更深入的解释

More in-depth explanation

Python 2下更好的通用解决方案将stdout看作是什么:一个8位接口。这意味着任何你打印到stdout应该是8位。当您尝试打印Unicode数据时,会出现错误,因为打印将尝试将Unicode数据编码为stdout的编码,如果为None,则将采用ASCII并失败,除非您设置PYTHONIOENCODING。

A better generic solution under Python 2 is to treat stdout as what it is: An 8-bit interface. And that means that anything you print to to stdout should be 8-bit. You get the error when you are trying to print Unicode data, because print will then try to encode the Unicode data to the encoding of stdout, and if it's None it will assume ASCII, and fail, unless you set PYTHONIOENCODING.

但是通过打印编码数据,你没有这个问题。即使当输出为管道时,以下工作也完美:

But by printing encoded data, you don't have this problem. The following works perfectly even when the output is piped:

print u'ÅÄÖ'.encode('UTF8')

(但是,这将失败在Python 3下,因为在Python 3下,stdout不再是8位IO,你应该给它的Unicode数据,它会自己编码。如果你给它二进制数据,它将打印表示。因此在Python 3你没有这个问题在第一)。

(However, this will fail Under Python 3, because under Python 3, stdout is no longer 8-bit IO, you are supposed to give it Unicode data, and it will encode by itself. If you give it binary data, it will print the representation. Therefore on Python 3 you don't have this problem in the first place).

这篇关于在Python中的Stdout编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆