If you’ve ever tried reading a CSV file in Python and suddenly got this confusing error:
KeyError: '\ufeffName'
…you’re not alone.
This problem happens even when the CSV looks perfectly fine, especially if it was exported from Windows or Excel.
There’s an invisible character at the start of the file that Python reads — but you don’t.
Let’s break down the issue and the one-line fix.
The Hidden Problem: BOM (\ufeff)
Many CSV files created on Windows tools include a Byte Order Mark (BOM).
It’s invisible, but Python treats it as part of the header.
Example Problem:
CSV file (data.csv):
Name,Age,Email
Alice,25,alice@example.com
Bob,30,bob@example.com
Charlie,28,charlie@example.com
If this CSV has a BOM, Python might read the first header as
\ufeffName
instead of Name, causing key errors:
import csv
with open("data.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row["Name"])
Output:
KeyError: '\ufeffName'
The One-Line Fix: Use utf-8-sig
Python has a specific encoding that automatically removes the BOM while reading:
utf-8-sig
Just open your file like this:
import csv
with open("data.csv", "r", encoding="utf-8-sig") as file:
reader = csv.DictReader(file)
for row in reader:
print(row["Name"], row["Age"], row["Email"])
That’s it. Problem solved.
Output:
Alice 25 alice@example.com
Bob 30 bob@example.com
Charlie 28 charlie@example.com
Why This Works
CSVs exported from Excel often include a BOM
The BOM is invisible but breaks Python’s header parsing utf-8-sig automatically removes the BOM before DictReader sees it
Your script works with any CSV source — Linux, Windows, Mac
When Should You Use utf-8-sig?
Use it whenever your CSV comes from:
- Excel
- Windows-based exports
- Enterprise systems
- Unknown or mixed data sources
It’s a safe default and won’t break anything even if the file contains no BOM.
Summary
If you encounter weird CSV header issues or errors like:
KeyError: '\ufeffName'
just open your CSV like this:
open("file.csv", "r", encoding="utf-8-sig")
This one line removes BOM automatically and makes your CSV reading code clean, stable, and cross-platform.
Top comments (0)