Registry / serialization / cchardet

cchardet

JSON →
library2.1.7pypypi✓ verified 23d ago

cChardet is a high-speed universal character encoding detector implemented as a C extension for Python. It provides fast and accurate detection of text encoding, leveraging the underlying `uchardet` library (a port of Mozilla's `chardet`). The current stable version is 2.1.7, with alpha releases for 2.2.0 indicating ongoing development and support for newer Python versions.

pip install cchardet
INSTALL
IMPORT
SIG · CCHARDET
C
cchardet
serializationpythonv2.1.7
Install
1.8s avg
Import
Disk
19MB
Pass rate
1/ 10
Env Coverage1 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.7 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✕ build_error
py 3.11
✕ build_error
✕ build_error
py 3.12
✕ build_error
✕ build_error
py 3.13
✕ build_error
✕ build_error
py 3.9
✕ build_error
✓ 1.8s
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

detect
import cchardet cchardet.detect(b'some bytes')
from cchardet import detect
While 'from cchardet import detect' technically works, the primary API usually involves 'import cchardet' and then accessing methods from the module directly, like cchardet.detect().
UniversalDetector
from cchardet import UniversalDetector
Used for streaming detection of encoding.

This quickstart demonstrates basic character encoding detection using `cchardet.detect()` for a single byte string and `UniversalDetector` for streaming data. `detect()` returns a dictionary with 'encoding', 'confidence', and 'language'.

import cchardet # Example 1: Detect encoding of a simple byte string data = 'これは日本語です'.encode('shift_jis') result = cchardet.detect(data) print(f"Detected encoding: {result['encoding']}, confidence: {result['confidence']:.2f}") # Example 2: Using UniversalDetector for streaming data from cchardet import UniversalDetector detector = UniversalDetector() for line in [b'Hello, world!', b'\xcf\x84\xce\xb7\xce\xbd \xce\xba\xce\xb1\xce\xbb\xce\xb7\xce\xbc\xce\xb5\xce\xbd \xcf\x81\xce\xb1!']: detector.feed(line) detector.close() streaming_result = detector.result print(f"Streaming detected encoding: {streaming_result['encoding']}, confidence: {streaming_result['confidence']:.2f}")
Debug
Known issues
breakingPython version support has significantly changed across major and minor releases. Version 2.1.6 dropped Python 2.7. Version 2.1.7 dropped Python 3.5. Future 2.2.x alpha releases indicate dropping support for Python 3.6-3.8 in favor of Python 3.10, 3.11, and 3.12. Always check the required Python version for the specific `cchardet` release you intend to use.
fix
Ensure your Python environment meets the requirements of the specific `cchardet` version. For new projects, target Python 3.9+ with `cchardet` 2.1.7, or consider using the 2.2.x series for newer Python versions once stable.
affects: >=2.1.6
breakingVersion 2.0.0 replaced the underlying `uchardet-enhanced` library with `uchardet`. While both are based on Mozilla's chardet, this change could potentially introduce subtle differences in detection results for specific edge cases or less common encodings.
fix
If upgrading from pre-2.0.0 versions, thoroughly test your application's encoding detection behavior, especially with diverse input data, to ensure consistency and correctness.
affects: >=2.0.0
gotcha`cchardet` is a C extension. If pre-built wheels are not available for your specific operating system, Python version, and architecture, `pip` will attempt to compile it from source. This requires a C compiler (e.g., GCC, Clang, MSVC) to be installed and properly configured on your system.
fix
Ensure you have a C compiler installed if you encounter build errors during `pip install`. For Windows, this typically means installing 'Build Tools for Visual Studio'. For Linux, 'build-essential' or similar packages. Consider using Docker or virtual environments with pre-built images if compilation is problematic.
affects: All versions
gotchaThe `detect` function can sometimes return a lower confidence for ambiguous encodings. A high confidence (e.g., >0.9) generally indicates a reliable detection, but lower confidence values might warrant further validation or fallback mechanisms.
fix
Always inspect the `confidence` value in the returned dictionary. Implement logic to handle cases where confidence is low, such as attempting other detection methods, using a default encoding, or prompting the user.
affects: All versions
Upgrade
Version history
2.1.7latest on PyPI · released Oct 27, 2020
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources
cchardet — pip install cchardet · libregistry