我正在打印文件的第一行:
with open(path,"r",encoding='utf8') as f:
for i, l in enumerate(f.readlines()):
print(bytes(l.rstrip(), encoding='utf8'))
我希望输出 b'1' 但我得到的是:
b'\xef\xbb\xbf1'
这里有什么问题?
我在 Windows 上,我使用 Notepad++ 保存我的文件。
最佳答案
要正确解析 BOM,请使用 utf-8-sig :
with open(path,"r",encoding='utf-8-sig') as f:
关于python - 文件第一行中包含的 unicode header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042173/