gpt4 book ai didi

python - 为什么 Python 3 默认读取编码为 cp1252 的文件?

转载 作者:可可西里 更新时间:2023-11-01 11:23:03 29 4
gpt4 key购买 nike

做的时候:

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

我得到:

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

为什么默认是cp1252test.txt 已使用 UTF8 编码保存,.py 脚本也已保存。

最佳答案

直接来自 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.

我的黑体

如果你想读取为 UTF-8,你只需使用参数:

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

关于python - 为什么 Python 3 默认读取编码为 cp1252 的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57624178/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com