all Python 3codecsterm
codecs.BOM_UTF16_LE
byte order marks (BOM)
The bytes b'\xff\xfe', the UTF-16 little-endian byte order mark. The same object is available as codecs.BOM_LE.
Syntax
codecs.BOM_UTF16_LE # b'\xff\xfe'
The 'utf-16-le' codec neither writes nor strips a BOM; it is for protocols where the order is fixed in advance, while 'utf-16' writes a BOM and uses it to detect order when reading. The same two bytes begin BOM_UTF32_LE, so when sniffing a file's encoding, check UTF-32 first and UTF-16 second. codecs.BOM_LE and the legacy BOM32_LE are other names for this very object.
Examples
import codecs
print(codecs.BOM_UTF16_LE, codecs.BOM_LE is codecs.BOM_UTF16_LE) # → b'\xff\xfe' True
print('hi'.encode('utf-16-le').startswith(codecs.BOM_UTF16_LE)) # → False
print(codecs.BOM_UTF32_LE.startswith(codecs.BOM_UTF16_LE)) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy