我在處理來自QProcess的unicode輸出時遇到了一些麻煩。當我運行下面的例子時,我得到了?而不是中文。任何人都可以告訴我如何獲得unicode輸出?通過QProcess打印unicode
from PyQt4.QtCore import *
def on_ready_stdout():
byte_array = proc.readAllStandardOutput()
print 'byte_array: ', byte_array
print 'unicode: ', unicode(byte_array)
proc = QProcess()
proc.connect(proc, SIGNAL('readyReadStandardOutput()'), on_ready_stdout)
proc.start(u'python -c "print \'hello 中文\'"')
proc.waitForFinished()
@serge 我試圖運行修改後的代碼,但我得到一個錯誤:
byte_array: hello Σ╕¡µ??
unicode:
Traceback (most recent call last):
File "python_temp.py", line 7, in on_ready_stdout
print 'unicode: ', unicode(byte_array)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 6: ordinal
not in range(128)
是您的程序文件的文件格式,目前UTF-8? – 2010-09-06 19:46:55
是的。 _____________________ – 2010-09-06 19:57:07