How to fix UnicodeDecodeError 'ascii' codec can't decode byte
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UnicodeDecodeError: 'ascii' codec can't decode byte ... means Python is trying to turn bytes into text with the ASCII codec, but the data contains characters outside the ASCII range. The fix is not to "remove the bad byte." The real fix is to decode the bytes using the encoding the data actually uses, usually UTF-8, Latin-1, or another known encoding.
Why The Error Happens
Bytes are not text. They become text only after Python decodes them with a character encoding. ASCII supports only 128 characters, so any byte sequence representing accented letters, emoji, or non-English scripts will fail under ASCII.
A minimal example shows the problem.
The last line raises the error because é is not representable in ASCII.
That is the central point: the bytes are probably fine. The chosen decoder is wrong.
Fix It By Using The Correct Encoding
If you know the data is UTF-8, decode it as UTF-8.
The same rule applies when opening files. Always specify the expected encoding instead of relying on a default.
If the file is not UTF-8, use the real encoding instead.
When working with HTTP responses, database exports, or CSV files from legacy systems, checking the documented encoding is usually the fastest path to a correct fix.
When You Do Not Know The Encoding
If the source encoding is unknown, start by checking metadata or the producing system. Guessing should be a fallback, not your first step.
For ad hoc debugging, you can inspect a file with a library such as chardet.
That gives you an estimate, not a guarantee. It is good for investigation, but production code should prefer a known encoding contract whenever possible.
Handling Bad Data Deliberately
Sometimes you only need the program to continue even if a few bytes are invalid. In that case, Python lets you choose an error strategy.
ignore silently drops invalid bytes. replace inserts a replacement character. These options are useful for rough diagnostics or non-critical logs, but they can corrupt data if used carelessly.
A better long-term fix is still to identify the correct encoding or clean the data at the source.
Bytes Versus Strings In Python 3
Many encoding bugs happen because code mixes bytes and str without noticing. In Python 3:
- '
bytesare raw binary data' - '
stris Unicode text'
You decode bytes into str, and you encode str into bytes.
Once that distinction is clear, many UnicodeDecodeError problems become easier to diagnose.
Common Pitfalls
A common mistake is trying random encodings until the error disappears. That may avoid the exception while silently corrupting text.
Another mistake is opening files without an explicit encoding= argument. That makes behavior depend on environment defaults, which can change between machines.
Developers also often use errors="ignore" as a permanent fix. It hides the exception, but it also drops data.
Finally, do not confuse decoding and encoding. decode() turns bytes into text. encode() turns text into bytes. Using the wrong operation at the wrong stage creates more confusion.
Summary
- The error means Python is decoding bytes with ASCII even though the data is not ASCII.
- Fix the problem by using the correct source encoding, often UTF-8.
- Specify
encoding=explicitly when opening text files. - Use detection tools only when the real encoding is unknown.
- Treat
errors="ignore"anderrors="replace"as fallback strategies, not ideal fixes. - Keep the
bytesversusstrdistinction clear in Python 3 code.
Related reading
- How to fixNameError name 'load_model' is not defined
- How to flatten a hierarchical index in columns
- How to flatten only some dimensions of a numpy array
- How to form tuple column from two columns in Pandas
- How to fix 'ValueError Empty Training Data' error in tensorflow
- How to fix VM issue with minikube start ?
- How to format a floating number to fixed width in Python
- How to generate a temporary url to upload file to Amazon S3 with boto library?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.