gpt4 book ai didi

python - 文件第一行中包含的 unicode header

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:57 25 4
gpt4 key购买 nike

我正在打印文件的第一行:

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/

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