python - printing unicode through a QProcess -


I have a problem in handling unicode output from a QPrice when I run the following example, I see ?? 中文Can anyone tell me how to get the Unicode output instead? 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-RD_Stiredout) proc.start (U'Python -C "print \" Hello 中文 \ '' '') Proc. waitForFinished () < / Pre>

@serge I tried to run my modified code, but I get an error:

  byte_array: hello Σ ╕¡μ ?? Unicode: Traceback (last most recent call): file "python_temp.py", line 7, on_ready_stdout Unicode: 'Unicode:' in Unicode (byte_array) UnicodeDecodeError: 'ascii' can not decode byte 0xe4 in codec position 6: in the ordering range (128)  

I have changed my code a little and have expected output:

  byte_array: Hello 中文Unicode: Hello 中文  

My change has been made:

  1. I added # - < - coding: UTF-8 - - Magic comment (details)
  2. Below "U" string slogan Has been replaced with proc.start call

Your code with my changes is:

  # - * - coding: UTF-8 - * - PQT 4. Import from QyTor * def on_ready_stdout (): byte_array = proc.readAllStandardOutput () print 'byte_array:', byte_array print 'unicode:', unicode (byte_array) proc = QProcess (proccconnect (proc, signal ( Proc 'start' 


Comments