I want to print text as UTF-8 when piped (to, for example, a file), so on Python 3.7.3 on Windows 10 via PowerShell, I'm doing this:
import sys
if not sys.stdout.isatty():
sys.stdout.reconfigure(encoding='utf-8')
print("Mamma mia.")
When run as encodingtest.py > test.txt, test.txt then turns out to be this:
00000000 FF FE 4D 00 61 00 6D 00 6D 00 61 00 20 00 6D 00 ÿþM.a.m.m.a. .m.
00000010 69 00 61 00 2E 00 0D 00 0A 00 i.a.......
Mysteriously enough, it starts with FF FE, which is the byte-order marker for UTF-16-LE – and null bytes are printed between the characters (as UTF-16 would have it)! However, when I run it via CMD rather than PowerShell, it prints UTF-8 just fine. How do I get Python to print UTF-8 even when piped via PowerShell?
I could run encodingtest.py | Out-File -Encoding UTF8 test.txt instead, but is there a way to ensure the output encoding program-side?