默认编码的Python标准错误? [英] Default encoding for python for stderr?

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

问题描述

我已经得到了我想要(使用bash BTW)其标准错误输出定向到/ dev / null的沉默嘈杂的Python脚本。

I've got a noisy python script that I want to silence by directing its stderr output to /dev/null (using bash BTW).

像这样:

python -u parse.py  1> /tmp/output3.txt 2> /dev/null

但很快退出prematurely。嗯。我看不到出去与标准错误,因为课程的回溯。它运行大肆,通常如果我不直接标准错误的地方。

but it quickly exits prematurely. Hmm. I can't see the traceback because of course that goes out with stderr. It runs noisily and normally if I don't direct stderr somewhere.

让我们尝试重定向到一个文件,而不是某个地方的/ dev / null,并且看看它的输出:

So let's try redirecting it to a file somewhere rather than /dev/null, and take a look at what it's outputting:

python -u parse.py  1> /tmp/output3.txt 2> /tmp/foo || tail /tmp/foo

Traceback (most recent call last):
  File "parse.py", line 79, in <module>
    parseit('pages-articles.xml')
  File "parse.py", line 33, in parseit
    print >>sys.stderr, "bad page title", page_title
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

所以,这是正在生成标准错误包含UTF8,出于某种原因,蟒蛇拒绝当它被重定向到打印非ASCII,即使它被定向到/ dev / null的(当然Python不知道)

So, the stderr that's being generated contains utf8, and for some reason python refuses to print non-ascii when it's being redirected, even though it's being directed to /dev/null (though of course python doesn't know that).

我怎么能沉默,即使它包含UTF8 python脚本的标准错误?有没有办法做到这一点,而无需重新编写每个打印到stderr在此脚本?

How can I silence the stderr of a python script even though it contains utf8? Is there any way to do it without re-writing every print to stderr in this script?

推荐答案

您可以通过沉默它绑定到一个自定义的作家标准错误:

You can silence stderr by binding it to a custom writer:

#!/usr/bin/env python
import codecs, sys

class NullWriter:
    def write(self, *args, **kwargs):
        pass

if len(sys.argv) == 2:
   if sys.argv[1] == '1':
      sys.stderr = NullWriter()
   elif sys.argv[1] == '2':
      #NOTE: sys.stderr.encoding is *read-only* 
      #      therefore the whole stderr should be replaced
      # encode all output using 'utf8'
      sys.stderr = codecs.getwriter('utf8')(sys.stderr)

print >>sys.stderr, u"\u20AC" # euro sign
print "ok"

例如:

$ python silence_stderr.py
Traceback (most recent call last):
  File "silence_stderr.py", line 11, in <module>
    print >>sys.stderr, u"\u20AC"
UnicodeEncodeError: 'ascii' codec can't encode character u'\u20ac' in position 0: ordinal not in range(128)

静音标准错误:

$ python silence_stderr.py 1
ok

恩codeD标准错误:

Encoded stderr:

$ python silence_stderr.py 2
€
ok

注意:我有在Emacs上面的输出,因此效仿它,你可以做一个终端:

NOTE: I've got the above outputs inside emacs therefore to emulate it in a terminal you could do:

$ python ... 2>out.txt
$ cat out.txt

注意:深入了解Windows控制台(后 CHCP 65001 中切换到UTF-8和TrueType字体(龙力控制台))我有奇怪的结果:

NOTE: Inside Windows console (after chcp 65001 that switch to 'utf-8' and with truetype font (Lucida Console)) I've got strange results:

C:\> python silence_stderr.py 2
Traceback (most recent call last):
  File "silence_stderr.py", line 14, in <module>
    print >>sys.stderr, u"\u20AC" # euro sign
  File "C:\pythonxy\python\lib\codecs.py", line 304, in write
    self.stream.write(data)
IOError: [Errno 13] Permission denied

如果该字体没有那么的TrueType异常不提高,但输出是错误的。

If the font is not truetype then the exception doesn't raise but the output is wrong.

Perl的工程TrueType字体:

Perl works for the truetype font:

C:\> perl  -E"say qq(\x{20ac})"
Wide character in print at -e line 1.
€

重定向工作虽然:

Redirection works though:

C:\>python silence_stderr.py 2 2>tmp.log
ok
C:\>cat tmp.log
€
cat: write error: Permission denied

重新注释

codecs.getwriter 文档:

中查找codeC对于给定的
  编码并返回它的StreamWriter
  类或工厂函数。提出了一个
   LookupError 的情况下,编码
  无法找到。

Look up the codec for the given encoding and return its StreamWriter class or factory function. Raises a LookupError in case the encoding cannot be found.

这是过度简化的观点:

class UTF8StreamWriter:
    def __init__(self, writer):
        self.writer = writer
    def write(self, s):
        self.writer.write(s.encode('utf-8'))

sys.stderr = UTF8StreamWriter(sys.stderr)

这篇关于默认编码的Python标准错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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