从包含多个BOM的文件中删除所有BOM

时间:2016-06-10 14:41:59

标签: python byte-order-mark

我有一个文本文件,其中包含以字节顺序标记开头的多行。将encoding='utf-8-sig'传递给open会删除文件开头的BOM,但所有后续的BOM都会保留。是否有更正确的方法来删除这些:

import codecs

filepath = 'foo.txt'
bom_len = len(codecs.BOM_UTF8)

def remove_bom(s):
    s = str.encode(s)

    if codecs.BOM_UTF8 in s:
        s = s[bom_len:]

    return s.decode()

try:
    with open(filepath, encoding='utf-8-sig') as file_object:
        for line in file_object:
            line = line.rstrip()
            line = remove_bom(line)
            if line != '':
                print([line[0]])
except FileNotFoundError:
    print('No file found at ' + filepath)

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。 这有点帮助了我:

import codecs
with open(path, "rb") as infile:
    bytecontent = infile.read()
bytecontent = bytecontent.replace(codecs.BOM_UTF8, b"")