print() function with encoding= and errors= parameters?

Malcolm Greene python at bdurham.com
Wed Aug 3 11:35:35 EDT 2016


Thank you Random832 and Peter - excellent ideas.

My use case was diagnostic output being (temporarily) output to stdout
via debug related print statements. The output is for debugging only and
not meant for production. I was looking for a solution that would allow
me to output to the console with as few changes to the original scripts
as possible, eg. non-invasive except for the print statements
themselves.

When debugging under Linux/OSX, standard print statements work fine
because their stdouts' encoding is UTF-8. But under Windows, the stdout
is workstation specific and *never* UTF-8. So the occasional non-ASCII
string trips up our diagnostic output when tested under Windows.

Peter's suggestion to set the PYTHONIOENCODING [1] environment variable
is the non-invasive, diagnostic only, non-production solution I was
looking for ... for the use case at hand. 

Again, thank you both.

Malcolm

[1] PYTHONIOENCODING=ascii:backslashreplace



More information about the Python-list mailing list