Using python 3, running the following code
print("some box drawing:")
print("┌─┬┼┴┐")
via
py my_app.py
prints
some box drawing:
┌─┬┼┴┐
As you would expect.
However, if you redirect this (either Windows or Linux) with
py my_app.py > redirected.txt
you get the following exception:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to <undefined>
As has been suggested in many other posts, this exception can be "fixed" by calling sys.stdout.reconfigure(encoding='utf-8') prior to printing. On linux and in the windows cmd, thats it, problem solved. Using PowerShell on Windows however, the output looks like this:
some box drawing:
ΓöîΓöÇΓö¼Γö╝Γö┤ΓöÉ
Which is especially odd, since it works fine using the cmd.exe console.
The code base is delivered to a customer as an executable and I would like to not ask them to execute something in the console in order for my program to work reliably. Is there a programmatic way to have box drawing characters written correctly when redirecting output to a file using the windows PowerShell?
From this answer, I have learned, that redirecting in the PowerShell to utf-8 simply does not work, but utf-16 does. Executing the following code on startup worked for me with/without redirect and in a number of different consoles:
I decided to only set the encoding when running a redirect and only to utf-16 when in the PowerShell as I wanted to avoid running into other unforeseen encoding problems with other setups: The snippet that detects the power shell from is taken from this answer and the snippet for detecting a redirect from this answer.
I myself find this solution a little messy. If you find a better solution, I am happy to accept it.