更新时间:2023-11-27 18:28:46
自您声明:
我所有的(文本)文件当前都在 与BOM一起存储在UTF-8中
All of my (text) files are currently stored in UTF-8 with the BOM
然后使用"utf-8-sig"编解码器对其进行解码:
then use the 'utf-8-sig' codec to decode them:
>>> s = u'Hello, world!'.encode('utf-8-sig')
>>> s
'\xef\xbb\xbfHello, world!'
>>> s.decode('utf-8-sig')
u'Hello, world!'
它会自动删除预期的BOM,并且如果该BOM也不存在,则可以正常工作.
It automatically removes the expected BOM, and works correctly if the BOM is not present as well.