chardet (Python)
| Language | Python (3.10+ for version 7) |
|---|---|
| License | 0BSD for version 7 (earlier versions LGPL-2.1; the relicensing of the rewrite is disputed, see the project's issue tracker) |
| Repository | github.com/chardet/chardet |
| Documentation | chardet.readthedocs.io |
| Depends on | nothing |
| Read / write | detects 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#
encodingisNonewhen nothing fits (for example binary data);mime_typethen often tells you what the file is.- Pure ASCII input is reported as
ascii, which is also valid UTF-8. detectexamines a limited number of bytes by default; passmax_bytes=len(data)to examine everything.