I'm aware that questions like this have been asked before. But I'm not finding a solution.
I want to use a unicode literal, defined in my python file, with the subprocess module. But I'm not getting the results that I need. For example the following code
# -*- coding: utf-8 -*-
import sys
import codecs
import subprocess
cmd = ['echo', u'你好']
new_cmd = []
for c in cmd:
if isinstance(c,unicode):
c = c.encode('utf-8')
new_cmd.append(c)
subprocess.call(new_cmd)
prints out
ä½ å¥½
If I change the code to
# -*- coding: utf-8 -*-
import sys
import codecs
import subprocess
cmd = ['echo', u'你好']
new_cmd = []
for c in cmd:
if isinstance(c,unicode):
c = c.encode(sys.getfilesystemencoding())
new_cmd.append(c)
subprocess.call(new_cmd)
I get the following
??
At this stage I can only assume I'm, repeatedly, making a simple mistake. But I'm having a hard time figuring out what it is. How can I get echo to print out the following when invoked via python's subprocess
你好
Edit:
The version of Python is 2.7. I'm running on Windows 8 but I'd like the solution to be platform independent.