I'm using the logging facility of Python and had the same problem that it transforms the correct end-of-line sequence "\x0D\x00\x0A\x00
" to "\x0D\x0A\x00
".
The program output which I want to log via Python is UTF-16
as you can see.
To avoid that Python does anything different from just writing the Bytes it receives from stdout to the logfile, I tried to add encoding="UTF-8"
to logging.FileHandler()
.
The result is that '\x0D
' isn't printed anymore and I get "\x0A\x00\x0A\x00
" instead. That's a little bit better I guess.
On Linux it seems that there is no problem with that. I'm using the same script there but my program prints UTF-8
characters instead.
(There are two questions left here: Why does Python add another newline? And is it possible to log additional Information to the logfile? Putting an 'U' before strings or using "...".encode(UTF-16) doesn't work. But otherwise the logfile will contain strings of different encodings and becomes useless...)