I have a very strange and frustrating problem here.
Consider a python file, say test_char.py
, with a single line in it:
print(u'\u2212')
If I were to run this file from the command line, everything goes fine and it prints out the proper character:
−
But, if I want to programatically examine the output of this file by running it with subprocess, as I might with the following code:
import subprocess
print(subprocess.run("python test_char.py", stdout=subprocess.PIPE))
The file itself produces the following traceback error:
Traceback (most recent call last):
File "test_char.py", line 1, in <module>
print(u'\u2212')
File "C:\<blahblahblah>\Python\Python36\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2212' in position 0: character maps to <undefined>
I have no idea how to work around this, and it's driving me crazy because I can't examine the output of files that print out these types of characters.