python - 为什么以两种不同的编码打开文件可以按预期工作?

标签 python python-3.x encoding character-encoding text-files

引用自here ,

The default encoding is platform-dependent, so this code might work on your computer (if your default encoding is utf-8), but then it will fail when you distribute it to someone else (whose default encoding is different, like CP-1252).

上面引用中提到的代码:

fp = open('text.txt') # Assuming file exists
a_string = file.read()

我在当前目录中创建了一个名为text.txt的文件(内容随机),其编码为“ANSI 1252”(使用notepad++检查)。我已经使用检查了我的系统(Windows)的默认编码

import locale
print(locale.getpreferredencoding())

给出输出

cp1252

读取文件的代码(我在引用下方提供)按预期工作。即使我使用它也能工作

fp = open('text.txt', encoding='utf-8') # or `fp = open('text.txt', encoding='cp1252')`

上面的代码如何适用于两种不同的编码?它不应该给出 UnicodeDecodeError 或类似的东西吗?

最佳答案

仅当输入包含编码映射之外的字符时,解码才会失败。如果您的文件是纯 ASCII,则在两种情况下读取的方式完全相同。

关于python - 为什么以两种不同的编码打开文件可以按预期工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30726724/

相关文章:

python - 如何使用 TensorFlow 获得稳定的结果,设置随机种子

python-3.x - 如何使用 socketio 将数据异步发送到 Web 客户端?

python - python中的错误文本编码

c# - 在 C# (Visual Studio) 中将 WAV 文件转换为 FLAC

Python requirements.txt 使用本地git依赖

python - 列表列表的浅拷贝

python - 如何将速度图转换为流体流动图

python - 过滤掉 'N' 后跟任意数字组合的正则表达式

python-3.x - Pytorch 试图使 NN 接收到无效的参数组合

java - AudioFormat.Encoding 构造函数参数的含义是什么?