UnicodeDecodeError when opening a CSV from Excel
open("data.csv") crashes with UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9. The file came from Excel.
open("data.csv") crashes with UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9. The file came from Excel.
Comments 5
Join the discussion CommentAlready have an account? Log in
Excel often saves CSV in a Windows code page instead of UTF-8. Try:
or, if the file is "CSV UTF-8" from Excel:
utf-8-sigalso removes the invisible BOM at the start, which otherwise ends up glued to your first column name.cp1252 worked. 0xe9 was an é in a name.
The BOM thing bit me for ages. My first column was called "date" and I couldn't understand why
row["date"]failed.Best long-term fix: in Excel choose "CSV UTF-8" when saving. Then always use
utf-8-sigin Python.Always pass
encoding=explicitly. The default depends on your operating system, so code that works on a Mac fails on Windows.