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.

Answer from David Z on Stack Overflow
🌐
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
🌐
GitHub
github.com › bowmanjd › python-chardet-example
GitHub - bowmanjd/python-chardet-example
Associate code example for Character Encodings and Detection with Python, chardet, and cchardet.
Author: bowmanjd
🌐
GeeksforGeeks
geeksforgeeks.org › python › character-encoding-detection-with-chardet-in-python
Character Encoding Detection With Chardet in Python - GeeksforGeeks
July 23, 2025 - ... Input: data = b'\xff\xfe\x41\x00\x42\x00\x43\x00' Output: UTF-16 Explanation: Encoding is detected of the above given data. Below are some of the examples by which we can understand how ...
🌐
DEV Community
dev.to › bowmanjd › character-encodings-and-detection-with-python-chardet-and-cchardet-4hj7
Character Encodings and Detection with Python, chardet, and cchardet - DEV Community
February 9, 2026 - git clone https://github.com/bowmanjd/python-chardet-example.git cd python-chardet-example/ python3 -m venv .venv .
🌐
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', ...}
🌐
Clay-Technology World
clay-atlas.com › home › [python] use "chardet" package to determine the encoding of the file
[Python] Use "chardet" package to determine the encoding of the file - Clay-Technology World
July 2, 2021 - import chardet text = open('test_01.txt', 'rb').read() print(chardet.detect(text)) COPY ... To be careful is that you must select the “rb” mode to open thefile. After all, python is read by default using unicode, so if we can’t judge the ...
🌐
Medium
medium.com › analytics-vidhya › python-character-detection-chardet-27e46218f0bb
Python Character Detection — chardet | by Dawn Moyer | Analytics Vidhya | Medium
December 3, 2020 - I decided to use a random number generator to identify packages in the first 20 python packages downloaded according to Python Wheels.
Find elsewhere
🌐
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.
🌐
Plain English
python.plainenglish.io › character-encoding-detection-made-easy-with-chardet-in-python-dab2ac258840
Character Encoding Detection Made Easy with Chardet in Python | by Yancy Dennis | Python in Plain English
February 7, 2023 - import chardet def detect_encoding(file): detector = chardet.universaldetector.UniversalDetector() with open(file, "rb") as f: for line in f: detector.feed(line) if detector.done: break… ... New Python content every day.
🌐
Readthedocs
chardet.readthedocs.io
chardet 7.6.0 documentation
chardet is a universal character encoding detector for Python.
🌐
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.
🌐
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.