How To Pass Unicode Text Message Through Popen.communicate()?
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
Solution 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 osos.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)
Post a Comment for "How To Pass Unicode Text Message Through Popen.communicate()?"