After I learned about reading unicode files in Python 3.0 web script, now it’s time for me to learn using print() with unicode.
I searched for writing unicode, for example this question explains that you can’t write unicode characters to non-unicode console. However, in my case, the output is given to Apache and I am sure that it is capable of handling unicode text. For some reason, however, the stdout of my web script is in ascii.
Obviously, if I was opening a file to write myself, I would do something like
open(filename, 'w', encoding='utf8')
but since I’m given an open stream, I resorted to using
sys.stdout.buffer.write(mytext.encode('utf-8'))
and everything seems to work. Does this violate some rule of good behavior or has any unintended consequences?
I don’t think you’re breaking any rule, but
looks like it might be handier / less clunky.
Edit: per comments, this isn’t quite right — @Miles gave the right variant (thanks!):
Edit: if you can arrange for environment variable
PYTHONIOENCODINGto be set to utf8 when Apache starts your script, that would be even better, makingsys.stdoutbe set toutf8automatically; but if that’s unfeasible or impractical thecodecssolution stands.