chardet.detect() returns a dictionary which provides the encoding as the value associated with the key 'encoding'. So you can do this:
import chardet
rawdata = open(infile, 'rb').read()
result = chardet.detect(rawdata)
charenc = result['encoding']
The chardet documentation is not explicitly clear about whether text strings and/or byte strings are supposed to work with the module, but it stands to reason that if you have a text string you don't need to run character detection on it, so you should probably be passing byte strings. Hence the binary mode flag (b) in the call to open(). But chardet.detect() might also work with a text string depending on which versions of Python and of the library you're using, i.e. if you do omit the b you might find that it works anyway even though you're technically doing something wrong.
GitHub
github.com › chardet › chardet
GitHub - chardet/chardet: Python character encoding detector · GitHub
from chardet import detect_all from chardet.enums import EncodingEra data = "Москва является столицей Российской Федерации и крупнейшим городом страны.".encode("windows-1251") # All encoding eras are considered by default — 4 candidates across eras for r in detect_all(data): print(r["encoding"], round(r["confidence"], 2)) # Windows-1251 0.46 # MacCyrillic 0.42 # KZ1048 0.2 # ptcp154 0.2 # Restrict to modern web encodings — 1 confident result for r in detect_all(data, encoding_era=EncodingEra.MODERN_WEB): print(r["encoding"], round(r["confidence"], 2)) # Windows-1251 0.46
Author: chardet
PyPI
pypi.org › project › chardet
chardet · PyPI
from chardet import detect_all from chardet.enums import EncodingEra data = "Москва является столицей Российской Федерации и крупнейшим городом страны.".encode("windows-1251") # All encoding eras are considered by default — 4 candidates across eras for r in detect_all(data): print(r["encoding"], round(r["confidence"], 2)) # Windows-1251 0.46 # MacCyrillic 0.42 # KZ1048 0.2 # ptcp154 0.2 # Restrict to modern web encodings — 1 confident result for r in detect_all(data, encoding_era=EncodingEra.MODERN_WEB): print(r["encoding"], round(r["confidence"], 2)) # Windows-1251 0.46
Readthedocs
chardet.readthedocs.io › en › latest › usage.html
Usage - chardet 7.6.1.dev26+gf88488a2d documentation
Set to False to get raw Python codec names (e.g., "shift_jis_2004" instead of "SHIFT_JIS"). prefer_superset (default False) — remap legacy ISO/subset encodings to their modern Windows/CP superset equivalents (e.g., ASCII → Windows-1252, ISO-8859-1 → Windows-1252). # Default: chardet 5.x compatible names chardet.detect(data) # {'encoding': 'ascii', ...} # Raw Python codec names chardet.detect(data, compat_names=False) # {'encoding': 'ascii', ...} # Superset remapping with compat names chardet.detect(data, prefer_superset=True) # {'encoding': 'Windows-1252', ...} # Superset remapping with raw codec names chardet.detect(data, prefer_superset=True, compat_names=False) # {'encoding': 'cp1252', ...}
Readthedocs
chardet.readthedocs.io › en › 5.0.0
chardet — chardet 5.0.0 documentation
Character encoding auto-detection in Python. As smart as your browser.
Readthedocs
chardet.readthedocs.io
chardet 7.6.0 documentation
chardet is a universal character encoding detector for Python.
GitHub
github.com › bowmanjd › python-chardet-example › blob › main › detect.py
python-chardet-example/detect.py at main · bowmanjd/python-chardet-example
Contribute to bowmanjd/python-chardet-example development by creating an account on GitHub.
Author: bowmanjd
Readthedocs
chardet.readthedocs.io › en › 5.2.0
chardet — chardet 5.2.0 documentation
Character encoding auto-detection in Python. As smart as your browser.
Chardet
chardet.github.io
Redirecting to chardet documentation...
Redirecting to chardet.readthedocs.io
Read the Docs
app.readthedocs.org › projects › chardet
chardet - Read the Docs Community
Chardet: The Universal Character Encoding Detector for Python · Maintainers · Repository https://github.com/chardet/chardet · Versions 26 Builds 524 ·
Readthedocs
chardet.readthedocs.io › en › 4.0.0
chardet — chardet 3.0.4 documentation
Character encoding auto-detection in Python. As smart as your browser.
TheLinuxCode
thelinuxcode.com › home › character encoding detection with chardet in python (2026): practical patterns for bytes, files, and web content
Character Encoding Detection With Chardet in Python (2026): Practical Patterns for Bytes, Files, and Web Content – TheLinuxCode
February 1, 2026 - In 2026, I typically install Python tools in a project environment (venv/uv) rather than globally. If you’re using classic pip: ... The primary API is chardet.detect(data: bytes) -> dict.
Anaconda.org
anaconda.org › anaconda › chardet
chardet - anaconda | Anaconda.org
Chardet is a character encoding detector for Python. It supports a wide range of encodings, including ASCII, UTF variants, various East Asian encodings (EUC, ISO-2022, Shift_JIS), and many others.