all Python 3codecsterm
codecs.BOM_UTF32_LE
byte order marks (BOM)
The bytes b'\xff\xfe\x00\x00', the UTF-32 little-endian byte order mark. It starts with the same two bytes as BOM_UTF16_LE.
Syntax
codecs.BOM_UTF32_LE # b'\xff\xfe\x00\x00'
The main trap is check order: data.startswith(BOM_UTF16_LE) is also true for UTF-32 LE, so the file gets misdetected as UTF-16. Test from the longest mark to the shortest. UTF-32 itself is rare in practice: 4 bytes for every character. The legacy name BOM64_LE is the same object.
Examples
import codecs
def sniff(data):
for bom, enc in [(codecs.BOM_UTF32_LE, 'utf-32'), (codecs.BOM_UTF32_BE, 'utf-32'),
(codecs.BOM_UTF8, 'utf-8-sig'), (codecs.BOM_UTF16_LE, 'utf-16'),
(codecs.BOM_UTF16_BE, 'utf-16')]:
if data.startswith(bom):
return enc
return 'utf-8'
print(sniff(codecs.BOM_UTF32_LE + 'hi'.encode('utf-32-le'))) # → utf-32
print(sniff('hi'.encode('utf-16'))) # → utf-16
See also
Open in the full glossarydocs.python.orgReport an inaccuracy