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

    Janela de erro com a logo do Python
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    7 Common Python Errors and How to Fix Them

    Learn seven common Python mistakes, how to read error messages, and practical ways to fix indentation, type conversion, data structure,

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026
    logotipo do Python em azul e amarelo ao lado das palavras 'try except' em fonte amarela sobre um fundo azul-texturizado
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Python try and except: Complete Beginner Guide

    Learn Python try and except with specific exceptions, else, finally, raise, chaining, custom errors, input validation, files, requests, logging, and

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026
    Uso de f-strings para debug e formatação em Python
    Error Resolution
    Foto de perfil de Leandro Hirt da Academify

    Debug Python with f-Strings: Practical Guide

    Learn to debug Python with f-strings using the debug specifier, repr, type checks, number formatting, loops, functions, exceptions, logging, and

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026
    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