I want to print unicode text to console window without messing with its encoding. I want to replace the characters that could not be printed.
My environment:
- MS Windows 10
- MS Visual Studio 2015
- Python Tools for Visual Studio 2.2.3
- Python 3.4
The problem is when starting the script from MSVS using Python Tools:
sys.stdout.encodingindicates that the encoding isutf8- but
print('\u2013')will trigger error'charmap' codec can't encode character '\u2013' in position 0: character maps to <undefined>
When I start the script from cmd.exe, sys.stdout.encoding indicates correct encoding cp852, in this case the error is expected.
I basically want to do this:
def safePrint(text):
e = sys.stdout.encoding
print(text.encode(e, 'replace').decode(e))
so that the characters not supported are replaced, eg.:
safePrint(u'a \u2013 b')
- shall output
a ? bforcp852 - and
a – bforutf-8
How can I find what encoding is print implementation really using?