Python wrapper for libguess, a library to determine character encoding of a string.
Project description
libguess enables finding out in what encoding some text is for given language. This library is especially useful for short text strings that themselves don’t carry reliable encoding information, like ID3 tags in MP3 files. For example we can encode the same Japanese string in 3 different encodings (UTF-8, SHIFT-JIS, EUC-JP) and libguess will hopefully guess the encoding correctly just by looking at the first few bytes of the given string.
Functions
determine_encoding(in_string, region)
Determines encoding of a string for the given language region.
As an usage example we might see what happens when given Japanese text in 2 different encodings:
>>> import guess >>> guess.determine_encoding(u'\u3042'.encode('euc-jp'), guess.REGION_JP) 'EUC-JP' >>> guess.determine_encoding(u'\u3042'.encode('utf-8'), guess.REGION_JP) 'UTF-8'
The output string of this function can be given directly to iconv_open() C function and the resulting names should be compatible with the encoding string of str.decode() function:
>>> encoded_value = u'\u3042'.encode('shift-jis') >>> encoding = guess.determine_encoding(encoded_value, guess.REGION_JP) >>> encoding 'SJIS' >>> encoded_value.decode(encoding) u'\u3042'
In case the given region name is invalid or the underlying libguess_determine_encoding() call fails for any other reason, None value is returned:
>>> encoding = guess.determine_encoding("asdf", "UNKNOWN") >>> encoding is None True
Use REGION_* constants for region names.
validate_utf8(in_string)
Checks if the given string is a valid UTF-8 byte sequence.
This function is included here for the completeness with libguess interface. It gives more precise results for UTF-8 validity than for example functions in glib.
>>> import guess >>> guess.validate_utf8(u'\u3042'.encode('EUC-JP')) False >>> guess.validate_utf8(u'\u3042'.encode('UTF-8')) True
Region names
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file python-libguess-1.0.tar.gz
.
File metadata
- Download URL: python-libguess-1.0.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 704a41ddcc655de68b649eeb22c646098eaa228da1a0a4740aadc8c41daed97e |
|
MD5 | 09b1ff97228dc0011b23c745d8455f5e |
|
BLAKE2b-256 | be72469b33a973118be3639ad45b9080c79679e5c2111e7b6686f8eb78597670 |