All formats

chardet (Python)

LanguagePython (3.10+ for version 7)
License0BSD for version 7 (earlier versions LGPL-2.1; the relicensing of the rewrite is disputed, see the project's issue tracker)
Repositorygithub.com/chardet/chardet
Documentationchardet.readthedocs.io
Depends onnothing
Read / writedetects the encoding, language and MIME type; decoding uses Python's codecs

chardet started as a Python port of Mozilla’s universal charset detector and has been the standard Python detector for many years. Version 7 (2026) is a ground-up rewrite with the same API: a pipeline of BOM checks, magic-number checks for binary formats, byte validity filters and bigram language models. It’s much faster and more accurate than version 6, detects 99 encodings, and returns the language and a MIME type with every result.

The rewrite was relicensed from LGPL to 0BSD; the original author contests that the rewrite is independent enough to allow it. If that matters for your project, pin chardet<7 (LGPL) or use charset-normalizer (MIT).

Installation#

Shell
pip install chardet

Basic usage: detect the encoding of a file#

Python
import chardet

with open("unknown.txt", "rb") as f:
    data = f.read()

result = chardet.detect(data)
print(result)
# {'encoding': 'Windows-1252', 'confidence': 0.73, 'language': 'fr', 'mime_type': 'text/plain'}

if result["encoding"]:
    text = data.decode(result["encoding"])

How to see all candidates#

Python
for r in chardet.detect_all(data)[:5]:
    print(r["encoding"], round(r["confidence"], 2))

How to detect while reading a large file#

Python
from chardet import UniversalDetector

detector = UniversalDetector()
with open("big.log", "rb") as f:
    for line in f:
        detector.feed(line)
        if detector.done:
            break
result = detector.close()
print(result["encoding"])

How to limit the candidate encodings#

Python
# only consider these
chardet.detect(data, include_encodings=["utf-8", "windows-1252", "windows-1250"])

# only encodings used on the modern web (drops DOS, Mac and EBCDIC code pages)
from chardet.enums import EncodingEra
chardet.detect_all(data, encoding_era=EncodingEra.MODERN_WEB)

How to detect from the command line#

Shell
chardetect notes.txt
# notes.txt: utf-8 with confidence 0.99
chardetect --minimal notes.txt
# utf-8

Tips#