Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows encoding changed when redirecting output

Tags:

python

windows

Hi I have the following python file 'test.py':

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

when I run

py test.py

I get:

utf-8
utf-8

but when I run

py test.py > test.txt

or

py test.py | Out-File -FilePath test.txt -Encoding ASCII

I get from test.txt:

cp1252
utf-8

And when I run:

import sys, locale
print(sys.getdefaultencoding())
print(locale.getpreferredencoding())

I get:

utf-8
cp1252

Question:
May I know why this happen and what should I do so that the default encoding is utf-8 when redirecting?
Thanks

like image 472
Kallzvx Avatar asked Jul 01 '26 18:07

Kallzvx


1 Answers

May I know why this happen

Because the Python developers chose to do it like that. See the documentation:

On Windows, UTF-8 is used for the console device. Non-character devices such as disk files and pipes use the system locale encoding (i.e. the ANSI codepage).

What should I do so that the default encoding is utf-8 when redirecting?

Force the encoding. I've added the following to my programs that were plagued by this problem:

if os.name == "nt":
    sys.stdout.reconfigure(encoding="utf-8")

If you use stderr, you might want to reconfigure that as well.

like image 199
Roland Smith Avatar answered Jul 04 '26 09:07

Roland Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!