Python:如何检查文件是否编码为 UTF16

要确定文件是否编码为 UTF-16,你可以检查文件开头是否存在字节顺序标记(BOM)。UTF-16 编码的文件通常以 BOM 开头,小端序为 0xFEFF,大端序为 0xFFFE

is_utf16.py
def is_utf16(filename):
    with open(filename, 'rb') as file:
        start = file.read(2)
        return start in [b'\xff\xfe', b'\xfe\xff']

# Example usage
filename = 'test.txt'
if is_utf16(filename):
    print(f"The file '{filename}' is encoded as UTF-16.")
else:
    print(f"The file '{filename}' is NOT encoded as UTF-16.")

Check out similar posts by category: Python