python unicode处理print和sys.stdout.write之间的差异 [英] python unicode handling differences between print and sys.stdout.write

查看:40
本文介绍了python unicode处理print和sys.stdout.write之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我先说我已经看过这篇文章:奇怪的python使用 unicode 打印行为,但那里提供的解决方案(使用 PYTHONIOENCODING)对我不起作用.

这是我的问题:

Python 2.6.5(r265:79063,2010 年 4 月 9 日,11:16:46)[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] 在 linux2>>>a = u'\xa6'>>>打印一个﹒

不过效果很好:

<预><代码>>>>sys.stdout.write(a)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 ?UnicodeEncodeError: 'ascii' 编解码器无法对位置 0 中的字符 u'\xa6' 进行编码:序号不在范围内 (128)

抛出错误.我在顶部链接的帖子表明这是因为默认的控制台编码是ascii".但是,就我而言,它不是:

<预><代码>>>>sys.stdout.encoding'UTF-8'

那么对这里的工作以及如何解决这个问题有什么想法吗?

谢谢D.

解决方案

这是由于一个长期存在的错误 已在 python-2.7 中修复,但为时已晚,无法向后移植到 python-2.6.

文档指出,将 unicode 字符串写入文件时,应使用 file.encoding.但这并没有被 sys.stdout 认可,而是使用默认的 unicode 编码.这通常由 site 模块设置为ascii",但它可以使用 sys.setdefaultencoding 进行更改:

Python 2.6.7(r267:88850,2011 年 8 月 14 日,12:32:40)[GCC 4.6.2] 在 linux3 上>>>a = u'\xa6\n'>>>sys.stdout.write(a)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,位于 <module>UnicodeEncodeError: 'ascii' 编解码器无法编码字符 u'\xa6' ...>>>重新加载(系统).setdefaultencoding('utf8')>>>sys.stdout.write(a)γ

但是,更好的解决方案可能是替换 sys.stdout带包装器:

class StdOut(object):定义写(自我,字符串):如果是实例(字符串,unicode):string = string.encode(sys.__stdout__.encoding)sys.__stdout__.write(string)>>>sys.stdout = StdOut()>>>sys.stdout.write(a)γ

I'll start by saying that I've already seen this post: Strange python print behavior with unicode, but the solution offered there (using PYTHONIOENCODING) didn't work for me.

Here's my issue:

Python 2.6.5 (r265:79063, Apr  9 2010, 11:16:46)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
>>> a = u'\xa6'
>>> print a 
¦

works just fine, however:

>>> sys.stdout.write(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa6' in position 0: ordinal not in range(128)

throws an error. The post I linked to at the top suggests that this is because the default console encoding is 'ascii'. However, in my case it's not:

>>> sys.stdout.encoding
'UTF-8'

So any thoughts on what's at work here and how to fix this issue?

Thanks D.

解决方案

This is due to a long-standing bug that was fixed in python-2.7, but too late to be back-ported to python-2.6.

The documentation states that when unicode strings are written to a file, they should be converted to byte strings using file.encoding. But this was not being honoured by sys.stdout, which instead was using the default unicode encoding. This is usually set to "ascii" by the site module, but it can be changed with sys.setdefaultencoding:

Python 2.6.7 (r267:88850, Aug 14 2011, 12:32:40) [GCC 4.6.2] on linux3
>>> a = u'\xa6\n'
>>> sys.stdout.write(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec cant encode character u'\xa6' ...
>>> reload(sys).setdefaultencoding('utf8')
>>> sys.stdout.write(a)
¦

However, a better solution might be to replace sys.stdout with a wrapper:

class StdOut(object):
    def write(self, string):
        if isinstance(string, unicode):
            string = string.encode(sys.__stdout__.encoding)
        sys.__stdout__.write(string)

>>> sys.stdout = StdOut()
>>> sys.stdout.write(a)
¦

这篇关于python unicode处理print和sys.stdout.write之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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