Fix Python UnicodeDecodeError Simply

Updated on: June 3, 2026
Reading time: 3 minutes
Como resolver UnicodeDecodeError em arquivos Python

If you have been programming for any length of time, you have almost certainly encountered a UnicodeDecodeError stopping your script cold. This error occurs when Python tries to read a file or byte sequence and cannot convert the data into readable text. Python 3 defaults to UTF-8, but many files created on older Windows systems or exported from Excel use Latin-1 or CP1252. When the interpreter tries to decode a special character using the wrong rule, it raises UnicodeDecodeError. This guide shows the most practical fixes.

Think of encoding as a dictionary: the computer stores numbers, not letters. The encoding is the map that says number 65 means ‘A’. The error appears when your script uses one dictionary and the file was written with another. The three most common situations are reading text files (.txt, .csv, .log), processing data from external APIs, and working with legacy database exports.

Fix 1: always specify the encoding

The most direct solution is to explicitly pass the encoding parameter when opening files. This single habit eliminates the vast majority of UnicodeDecodeError occurrences. See also fixing Python UTF-8 encoding errors for a deeper dive into encoding concepts.

Python
# Basic solution: specify the encoding explicitly
with open("my_file.txt", "r", encoding="utf-8") as file:

Fix 2: auto-detect with chardet

When you do not know what encoding a file uses, the chardet library can detect it. Install it with pip install chardet:

Python
import chardet

Fix 3: try/except with fallback encoding

Python
try:

Fix 4: Pandas CSV with encoding parameter

CSV files exported from Excel in Portuguese/Spanish locales are commonly encoded as ISO-8859-1. Pass the encoding parameter directly to Pandas:

Python
import pandas as pd

Quick reference: encodings and when to use each

EncodingWhen to use
utf-8Default for all new files, web APIs, modern systems
latin-1 / iso-8859-1Old European files, legacy exports
cp1252Windows-generated files with special characters
utf-16Some Windows text files, Office exports

The rule is simple: always pass encoding='utf-8' when opening files. When dealing with files from external or legacy sources, use chardet to detect the encoding first, then pass it explicitly. For more on related import and file errors, see fixing Python ImportError.

Share:

Facebook
WhatsApp
Twitter
LinkedIn

Article content

    Related articles

    Como resolver problemas de caminhos de arquivos em scripts Python
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Fix Python FileNotFoundError: Path Guide

    Fix Python FileNotFoundError: understand absolute vs relative paths, use pathlib and os.getcwd(), handle errors with try/except, and build a reliable

    Ler mais

    Tempo de leitura: 3 minutos
    03/06/2026
    Como resolver erros de codificação UTF-8 em Python
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Fix Python UTF-8 Encoding Errors

    Fix Python UTF-8 encoding errors: always specify encoding when opening files, handle UnicodeDecodeError, auto-detect with chardet, and write a safe

    Ler mais

    Tempo de leitura: 4 minutos
    03/06/2026
    Como corrigir erro ModuleNotFoundError em Python rapidamente
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Fix Python ModuleNotFoundError Fast

    Fix Python ModuleNotFoundError: check missing packages, typos, virtual environments, sys.path issues, and file name conflicts with practical examples.

    Ler mais

    Tempo de leitura: 8 minutos
    03/06/2026
    Como corrigir ImportError em projetos Python rapidamente
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Fix Python ImportError in 2 Minutes

    Fix Python ImportError in 2 minutes: identify duplicate file names, circular imports, missing names, and use a diagnostic script to

    Ler mais

    Tempo de leitura: 4 minutos
    03/06/2026
    Como resolver RecursionError em Python rapidamente
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Fix Python RecursionError in 2 Minutes

    Fix Python RecursionError fast: add a base case, increase the recursion limit with sys, convert to loops, or use lru_cache

    Ler mais

    Tempo de leitura: 4 minutos
    30/05/2026
    Como identificar e corrigir erros de sintaxe em Python
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Fix Python SyntaxError Quickly

    Learn how to fix Python SyntaxError by reading tracebacks, checking indentation, missing colons, brackets, quotes, keywords, and common beginner mistakes.

    Ler mais

    Tempo de leitura: 9 minutos
    28/05/2026