Recognizing a UTF-8 BOM
A UTF-8 BOM is the byte sequence EF BB BF at the start of a text stream; inspect the first three bytes before decoding, and strip it unless the consuming format explicitly expects it.
The short answer
A UTF-8 BOM is the byte sequence EF BB BF, the UTF-8 encoding of U+FEFF, placed at the very beginning of a text stream. It is not visible text; it is a signature that some Windows tools add. Detect it by examining the first three bytes before decoding, and remove it for formats such as JSON, JSON Lines, CSV headers, configuration files, and executable scripts. Keep it only when a consumer explicitly expects it, such as some spreadsheet import paths or XML and HTML encoding detection.
A minimal example
from pathlib import Path
raw = Path("report.csv").read_bytes()
if raw.startswith(b"\xef\xbb\xbf"):
print("BOM present")
text = raw[3:].decode("utf-8")
else:
text = raw.decode("utf-8")
print(repr(text[:20]))
Python's utf-8-sig codec performs the same check and strips the BOM on read; on write it emits one:
from pathlib import Path
text = Path("report.csv").read_text(encoding="utf-8-sig") # strips one leading BOM
Path("clean.csv").write_text(text, encoding="utf-8") # writes no BOM
From a shell, inspect the first bytes and let file classify the stream:
head -c 3 report.csv | xxd
file report.csv
file typically reports UTF-8 Unicode (with BOM) text when the signature is present.
Why this happens
U+FEFF was designed as a byte order mark for UTF-16 and UTF-32, where byte order varies between systems. UTF-8 has a fixed byte order, so the BOM carries no ordering information; its only role is to label an otherwise unmarked stream as UTF-8. The Unicode FAQ notes that some recipients do not expect it and that it interferes with formats requiring specific ASCII bytes at the start, such as the #! line of a Unix script.
Damage also occurs after decoding. Read a BOM-prefixed file with plain utf-8 and the BOM becomes the first character of the string. A CSV header named id becomes \ufeffid, a JSON document begins with an unexpected character, and equality checks against "id" fail. Python's json module reports Unexpected UTF-8 BOM (decode using utf-8-sig).
What changes the answer
| Consumer | BOM handling |
|---|---|
| JSON (RFC 8259) | Senders MUST NOT add one; parsers MAY ignore it |
| JSON Lines | Must not be included |
| CSV | Not defined by RFC 4180; Excel often writes one, strict parsers keep it in the first field |
| XML, HTML | Allowed; participates in encoding detection |
| Unix executable script | Breaks the #! line |
| Plain UTF-8 text | Tolerated but discouraged |
The decision is about the consumer, not about UTF-8 itself. When your program writes files that another program parses, default to utf-8. When your program reads files produced by Windows tools, accept utf-8-sig on input and normalize to utf-8 internally.
Edge cases
- A BOM is U+FEFF only at the start of the stream. The same code point later in the text is a zero-width no-break space and is content; do not strip it globally.
utf-8-sigremoves only theEF BB BFsequence. It does not handle UTF-16 BOMs (FF FEorFE FF), and it removes only one leading BOM, not repeated ones.- HTTP
Content-Type: ...; charset=utf-8is the declared encoding signal. A BOM is not a substitute for a missing charset, and a server should not emit one for JSON APIs. - Concatenating two BOM-prefixed files produces a BOM in the middle of the result, where it is data rather than a signature.
- To remove a decoded BOM, delete exactly one leading
\ufeff; Python'sstrip()does not treat U+FEFF as whitespace, and broad character stripping can remove legitimate text.
Practical checks
- Keep one fixture with a BOM and one without, then assert that both parse to the same value.
- Compare raw bytes when diagnosing:
raw[:3] == b"\xef\xbb\xbf". - If decoding fails with invalid bytes rather than a stray U+FEFF, the problem is different; see Python UnicodeDecodeError for that case.
- For generated output, write with
encoding="utf-8"and verify the first three bytes in a test.
Sources
Revision history
- Published
- Updated
- — Initial publication.