Python
UnicodeDecodeError
ASCII
character encoding
error handling

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.

Browse interview questions

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.

python
raw = "café".encode("utf-8")
print(raw)
print(raw.decode("ascii"))

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.

python
raw = "café".encode("utf-8")
text = raw.decode("utf-8")
print(text)

The same rule applies when opening files. Always specify the expected encoding instead of relying on a default.

python
1with open("data.txt", "r", encoding="utf-8") as file:
2    text = file.read()
3
4print(text)

If the file is not UTF-8, use the real encoding instead.

python
1with open("legacy.txt", "r", encoding="latin-1") as file:
2    text = file.read()
3
4print(text)

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.

python
1import chardet
2
3with open("data.txt", "rb") as file:
4    sample = file.read()
5
6result = chardet.detect(sample)
7print(result)

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.

python
raw = b"hello\xffworld"
print(raw.decode("utf-8", errors="ignore"))
print(raw.decode("utf-8", errors="replace"))

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:

  • 'bytes are raw binary data'
  • 'str is Unicode text'

You decode bytes into str, and you encode str into bytes.

python
1text = "naïve"
2raw = text.encode("utf-8")
3round_trip = raw.decode("utf-8")
4
5print(type(text))
6print(type(raw))
7print(round_trip)

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" and errors="replace" as fallback strategies, not ideal fixes.
  • Keep the bytes versus str distinction clear in Python 3 code.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.