chardet: Universal Character Encoding Detector
chardet is a Python library that detects the character encoding of byte strings, providing the detected encoding, confidence score, and language. The current version is 7.4.0.post1, released on March 14, 2026. It is actively maintained with regular updates, focusing on improving accuracy and performance. The library requires Python 3.10 or higher and has zero runtime dependencies, making it suitable for various Python environments, including PyPy.
Common errors
-
ModuleNotFoundError: No module named 'chardet'
cause The `chardet` library is not installed in the Python environment being used, or the Python interpreter cannot find it due to incorrect path configurations or virtual environment issues.fixInstall the `chardet` library using pip: `pip install chardet` (or `pip3 install chardet` for Python 3 specific installations). -
TypeError: Expected object of type bytes or bytearray, got: <class 'str'>
cause The `chardet.detect()` function, or other functions within the library, expects a byte string (`bytes` or `bytearray`) as input, but a regular Python string (`str`) was provided.fixConvert the string input to bytes using an appropriate encoding (e.g., `some_string.encode('utf-8')`) before passing it to `chardet.detect()`. Example: `chardet.detect(your_string.encode('utf-8'))`. If reading from a file, open it in binary read mode (`'rb'`). -
chardet returns None for encoding
cause The `chardet.detect()` function returns `None` for the 'encoding' key when the provided data appears to be binary rather than text, or contains too many null bytes or control characters that prevent a confident text encoding detection.fixEnsure the input data is indeed text-like. If it's genuinely binary, `chardet` cannot detect a text encoding. If it is text, providing more data to `chardet.detect()` or using `chardet.detect_all()` to see other candidates might help.
Warnings
- breaking chardet 7.0 is a ground-up, MIT-licensed rewrite — same package name, same public API — drop-in replacement for chardet 5.x/6.x. Python 3.10+, zero runtime dependencies, works on PyPy.
- deprecated chardet 7.0.0 introduced a new dense zlib-compressed model format (v2) that significantly reduces cold start times. Ensure your environment supports this format for optimal performance.
Install
-
pip install chardet
Imports
- detect
from chardet import detect
Quickstart
import chardet
# Detect encoding of a byte string
result = chardet.detect(b'Hello, world!')
print(result)
# Output: {'encoding': 'ascii', 'confidence': 1.0, 'language': 'en'}