encutils

raw JSON →
1.0.0 verified Sat May 09 auth: no python

encutils is a Python utility library for encoding detection and conversion, providing simple functions to handle text encoding issues. Current version 1.0.0, released with Python >=3.10 support. Low release cadence.

pip install encutils
error TypeError: expected bytes-like object, not 'str'
cause Passing a string instead of bytes to detect_encoding or convert_encoding.
fix
Encode string to bytes: text.encode('utf-8') or use b'...' literal.
error UnknownEncoding: No encoding detected for input
cause Input is too short or has no recognizable encoding pattern.
fix
Provide longer input (at least 4 bytes) or try specifying encoding explicitly with convert_encoding(input, 'utf-8', fallback='latin-1').
gotcha Input must be bytes, not str. Passing str will raise TypeError.
fix Ensure input is bytes (e.g., my_str.encode('utf-8')) or bytes object.
deprecated The function guess_encoding is deprecated in favor of detect_encoding which provides more accurate detection.
fix Replace guess_encoding() with detect_encoding() for new code.

Detect encoding of a byte string and convert to UTF-8.

from encutils import detect_encoding, convert_encoding

text = b'Hello World'
encoding = detect_encoding(text)
print('Detected encoding:', encoding)

converted = convert_encoding(text, 'utf-8')
print('Converted text:', converted.decode('utf-8'))