Python Glossary Русский

all Python 3codecsterm

codecs.BOM_UTF16

byte order marks (BOM)

The UTF-16 BOM in the machine's native byte order: BOM_UTF16_LE on x86 and ARM. The 'utf-16' codec writes it; codecs.BOM is another name for the same object.

Syntax

codecs.BOM_UTF16  # b'\xff\xfe' на little-endian

Its value depends on the platform, so code that sniffs files compares against BOM_UTF16_LE and BOM_UTF16_BE explicitly rather than BOM_UTF16: a file from another machine may carry the other order. The constant is handy for checking that 'utf-16' output starts with the mark. codecs.BOM is the same object; on a little-endian machine it is also BOM_UTF16_LE.

Examples

import codecs
print(codecs.BOM is codecs.BOM_UTF16)  # → True
print(codecs.BOM_UTF16 in (codecs.BOM_UTF16_LE, codecs.BOM_UTF16_BE))  # → True
print('hi'.encode('utf-16')[:2] == codecs.BOM_UTF16)  # → True
print(codecs.BOM_UTF16)  # → ? b'\xff\xfe' — на little-endian машине

See also

Open in the full glossarydocs.python.orgReport an inaccuracy