Python读取文本文档时出现神奇的错误,如读取一个空文本文档却有\ufeff的值
Windows新建文本文档test.txt→敲7个回车→另存为→指定编码UTF-8
运行代码
with open('test.txt', encoding='utf-8') as f:
text = f.read().splitlines()
print(text)
# ['\ufeff', '', '', '', '', '']
用Notepad++打开观察到编码为UTF-8-BOM
而不是UTF-8
Notepad++→编码→使用 UTF-8 编码→保存