Skip to main content

Python · 5 common causes

UnicodeDecodeError: 'utf-8' codec can't decode byte

Updated

Python tried to decode bytes as UTF-8 and hit a sequence that is not valid UTF-8.

The exact message

Traceback (most recent call last):
  File "/app/load.py", line 3, in <module>
    text = open('export.csv').read()
           ^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is UnicodeDecodeError: 'utf-8' codec can't decode byte.

How to fix it

Cause 1 of 5

The file is not UTF-8 — commonly Windows-1252 or Latin-1 from a spreadsheet or legacy export.

Decode it with the encoding it actually uses: open(path, encoding='cp1252'). If you do not know, charset-normalizer or chardet will guess — treat the guess as a hypothesis and eyeball the result before running a bulk import on it.

Cause 2 of 5

The file is binary but was opened as text.

A failure at position 0 on byte 0xff, 0xfe, 0x89 or 0x50 usually means a real binary file — an image, a .xlsx, a gzip. Open it with mode 'rb' and hand it to the right library: pandas.read_excel or openpyxl for .xlsx, not open().read().

Cause 3 of 5

You relied on the platform default encoding.

open() without encoding= uses the locale encoding, which is UTF-8 on most Linux and macOS setups but has historically been cp1252 on Windows — so the same script behaves differently per machine. Always pass encoding='utf-8' explicitly.

Cause 4 of 5

The file starts with a UTF-8 byte order mark.

A BOM decodes to a stray character that breaks the first CSV header or JSON key, and shows up as a mysterious leading character in output. Open with encoding='utf-8-sig', which recognises and strips it.

Cause 5 of 5

You are scanning a large log and a few bad bytes are acceptable.

open(path, encoding='utf-8', errors='replace') substitutes a replacement character instead of raising; errors='ignore' drops the bytes. Both lose data silently, so use them for inspection and never for data you intend to store.

Stopping it coming back

Pass encoding= on every open() and normalise incoming files to UTF-8 at the boundary, so the rest of the codebase only ever deals with one encoding.

UnicodeDecodeError: 'utf-8' codec can't decode byte — common questions

What does the position number tell me?
It is the byte offset where decoding failed. Position 0 points at a BOM or a genuinely binary file; a position deep in the file usually means one accented name or a smart quote in otherwise ASCII data, which points at Latin-1 or cp1252.
Is Latin-1 a safe fallback?
It never raises, because every byte is a valid Latin-1 character — which is exactly the danger. It will happily turn mojibake into text you then store. Use it only to inspect the bytes, not to import data.
How do I write UTF-8 without a BOM?
open(path, 'w', encoding='utf-8') writes no BOM. Use 'utf-8-sig' only when a consumer specifically requires one — some older Windows tools do, and Excel reads CSV more reliably with it.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor