2

我使用匿名管道捕获所有标准输出,然后将标准错误打印到 Richedit 中,当我使用 wsprintf 时没问题,但是使用多字节字符的 python 真的让我很恼火。如何将所有这些输出转换为 unicode?

更新 2010-01-03:

感谢您的回复,但似乎str.encode()唯一可以使用的print xxx东西,如果在 期间出现错误py_runxxx(),我重定向的 stderr 将捕获多字节字符串中的错误消息,那么有没有办法可以让 python 以 unicode 方式输出它的消息?在这篇文章中似乎有一个可用的解决方案。

我稍后会尝试。

4

3 回答 3

9

首先,请记住,在 Windows 控制台上可能不完全支持 Unicode。

下面的示例确实使 python 输出到stderrstdout使用UTF-8。如果您愿意,可以将其更改为其他编码。

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import codecs, sys

reload(sys)
sys.setdefaultencoding('utf-8')

print sys.getdefaultencoding()

sys.stdout = codecs.getwriter('utf8')(sys.stdout)
sys.stderr = codecs.getwriter('utf8')(sys.stderr)

print "This is an Е乂αmp١ȅ testing Unicode support using Arabic, Latin, Cyrillic, Greek, Hebrew and CJK code points."
于 2010-01-04T19:54:37.600 回答
0

您可以通过将字符串标记为 Unicode(即:)u'Hello World'或使用所有字符串都具有的 encode() 方法在 python 中使用 Unicode。

例如。假设你有一个 Unicode 字符串,aStringVariable:

aStringVariable.encode('utf-8')

会将其转换为 UTF-8。'utf-16' 将为您提供 UTF-16,而 'ascii' 会将其转换为普通的旧 ASCII 字符串。

有关更多信息,请参阅:

于 2010-01-03T06:29:32.440 回答
-1

wsprintf?

这似乎是一个“C/C++”问题而不是 Python 问题。

Python 解释器总是将字节串写入 stdout/stderr,而不是 unicode(或“宽”)字符串。这意味着 Python 首先使用当前编码(可能sys.getdefaultencoding())对所有 unicode 数据进行编码。

如果您想将 stdout/stderr 作为 unicode 数据获取,则必须使用正确的编码自行解码。

你最喜欢的 C/C++ 库当然可以做到这一点。

于 2010-01-03T19:41:50.910 回答