| 1 |
Advanced Search |
| 2 |
=============== |
| 3 |
|
| 4 |
Charset Normalizer method ``from_bytes``, ``from_fp`` and ``from_path`` provide some |
| 5 |
optional parameters that can be tweaked. |
| 6 |
|
| 7 |
As follow :: |
| 8 |
|
| 9 |
from charset_normalizer import from_bytes |
| 10 |
|
| 11 |
my_byte_str = 'Bсеки човек има право на образование.'.encode('cp1251') |
| 12 |
|
| 13 |
results = from_bytes( |
| 14 |
my_byte_str, |
| 15 |
steps=10, # Number of steps/block to extract from my_byte_str |
| 16 |
chunk_size=512, # Set block size of each extraction |
| 17 |
threshold=0.2, # Maximum amount of chaos allowed on first pass |
| 18 |
cp_isolation=None, # Finite list of encoding to use when searching for a match |
| 19 |
cp_exclusion=None, # Finite list of encoding to avoid when searching for a match |
| 20 |
preemptive_behaviour=True, # Determine if we should look into my_byte_str (ASCII-Mode) for pre-defined encoding |
| 21 |
explain=False, # Print on screen what is happening when searching for a match |
| 22 |
language_threshold=0.1 # Minimum coherence ratio / language ratio match accepted |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
Using CharsetMatches |
| 27 |
------------------------------ |
| 28 |
|
| 29 |
Here, ``results`` is a ``CharsetMatches`` object. It behave like a list but does not implements all related methods. |
| 30 |
Initially, it is sorted. Calling ``best()`` is sufficient to extract the most probable result. |
| 31 |
|
| 32 |
.. autoclass:: charset_normalizer.CharsetMatches |
| 33 |
:members: |
| 34 |
|
| 35 |
List behaviour |
| 36 |
-------------- |
| 37 |
|
| 38 |
Like said earlier, ``CharsetMatches`` object behave like a list. |
| 39 |
|
| 40 |
:: |
| 41 |
|
| 42 |
# Call len on results also work |
| 43 |
if not results: |
| 44 |
print('No match for your sequence') |
| 45 |
|
| 46 |
# Iterate over results like a list |
| 47 |
for match in results: |
| 48 |
print(match.encoding, 'can decode properly your sequence using', match.alphabets, 'and language', match.language) |
| 49 |
|
| 50 |
# Using index to access results |
| 51 |
if results: |
| 52 |
print(str(results[0])) |
| 53 |
|
| 54 |
Using best() |
| 55 |
------------ |
| 56 |
|
| 57 |
Like said above, ``CharsetMatches`` object behave like a list and it is sorted by default after getting results from |
| 58 |
``from_bytes``, ``from_fp`` or ``from_path``. |
| 59 |
|
| 60 |
Using ``best()`` return the most probable result, the first entry of the list. Eg. idx 0. |
| 61 |
It return a ``CharsetMatch`` object as return value or None if there is not results inside it. |
| 62 |
|
| 63 |
:: |
| 64 |
|
| 65 |
result = results.best() |
| 66 |
|
| 67 |
Calling first() |
| 68 |
--------------- |
| 69 |
|
| 70 |
The very same thing than calling the method ``best()``. |
| 71 |
|
| 72 |
Class aliases |
| 73 |
------------- |
| 74 |
|
| 75 |
``CharsetMatches`` is also known as ``CharsetDetector``, ``CharsetDoctor`` and ``CharsetNormalizerMatches``. |
| 76 |
It is useful if you prefer short class name. |
| 77 |
|
| 78 |
Verbose output |
| 79 |
-------------- |
| 80 |
|
| 81 |
You may want to understand why a specific encoding was not picked by charset_normalizer. All you have to do is passing |
| 82 |
``explain`` to True when using methods ``from_bytes``, ``from_fp`` or ``from_path``. |