How to make python 3 print() utf8 How to make python 3 print() utf8 python-3.x python-3.x

How to make python 3 print() utf8


Clarification:

TestText = "Test - āĀēĒčČ..šŠūŪžŽ" # this not UTF-8...it is a Unicode string in Python 3.X.TestText2 = TestText.encode('utf8') # this is a UTF-8-encoded byte string.

To send UTF-8 to stdout regardless of the console's encoding, use the its buffer interface, which accepts bytes:

import syssys.stdout.buffer.write(TestText2)


This is the best I can dope out from the manual, and it's a bit of a dirty hack:

utf8stdout = open(1, 'w', encoding='utf-8', closefd=False) # fd 1 is stdoutprint(whatever, file=utf8stdout)

It seems like file objects should have a method to change their encoding, but AFAICT there isn't one.

If you write to utf8stdout and then write to sys.stdout without calling utf8stdout.flush() first, or vice versa, bad things may happen.


As per this answer

You can manually reconfigure the encoding of stdout as of python 3.7

import syssys.stdout.reconfigure(encoding='utf-8')