How to pass unicode text message through Popen.com

2019-08-16 17:04发布

问题:

I have python script which display Unicode message to the terminal while executing. So i want to display that message in the web page while the process runs. I was able to run the script through Popen package but its communicate() method which receives the output of the scripts can't receive Unicode text.

UnicodeEncodeError: \'charmap\' codec can\'t encode characters in position 12-15: character maps to <undefined>\r\n'

So i try to pass the message as bytes which can pass through it but i can't decode it later to discuss about this question go to: How to decode a string representation of a bytes object?

But Now in this post i want to know the way to pass Unicode text through Popen.communicate() My code here is

from subprocess import Popen, PIPE
scripts = os.path.join(os.getcwd(), 'scripts')
p = Popen(["python", "tasks.py"],bufsize=0, cwd=scripts, stdout=PIPE, stderr=PIPE)
out,err = p.communicate()

回答1:

The comment of @snakecharmerb is the solution.

You must set the PYTHONIOENCODING variable before launching the child Python process to allow it to correctly encode its output in UTF-8. Then you have just to decode what you get in the parent:

from subprocess import Popen, PIPE
import os

os.environ['PYTHONIOENCODING'] = 'UTF-8'
scripts = os.path.join(os.getcwd(), 'scripts')
p = Popen(["python", "tasks.py"],bufsize=0, cwd=scripts, stdout=PIPE, stderr=PIPE)
out,err = p.communicate()
print(out.decode())

If you have Python>=3.6, you can directly get out and err as unicode strings:

os.environ['PYTHONIOENCODING'] = 'UTF-8'
scripts = os.path.join(os.getcwd(), 'scripts')
p = Popen(["python", "tasks.py"],bufsize=0, cwd=scripts, stdout=PIPE, stderr=PIPE, encoding='UTF-8')
out,err = p.communicate()
print(out)