2

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.encoding indicates that the encoding is utf8
  • 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 ? b for cp852
  • and a – b for utf-8

How can I find what encoding is print implementation really using?

Pavel Machyniak
  • 1,051
  • 7
  • 10
  • Related, if not the same issue: http://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using – theB Jun 03 '16 at 15:52
  • @theB I don't think so, it is not python related and that is what I am asking... And my problem is probably related only to specific combination running python from Visual Studio via Python Tools. – Pavel Machyniak Jun 04 '16 at 20:06
  • This works fine for me in VS Community 2015 14.0.25123 Update 2, with PTVS 2.2.40315. – Eryk Sun Jun 07 '16 at 14:54

0 Answers0