简体   繁体   中英

Why does Python 3 read a file with encoding cp1252 by default?

When doing:

with open('test.txt', 'r') as f:
    print(f)

I get:

<_io.TextIOWrapper name='test.txt' mode='r' encoding='cp1252'>

Why is it cp1252 by default? The test.txt has been saved with UTF8 encoding, and the .py script too.

Directly from the documentation of open :

The default encoding is platform dependent (whatever locale.getpreferredencoding() returns ), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.

My bolding

If you want to read as UTF-8 you just use the argument:

with open('test.txt', 'r', encoding='utf-8') as f:
    print(f)

You can see your default system encoding this way:

import sys
sys.getdefaultencoding()

The you can decode and encode accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM