| 1 |
============== |
| 2 |
Miscellaneous |
| 3 |
============== |
| 4 |
|
| 5 |
Convert to str |
| 6 |
-------------- |
| 7 |
|
| 8 |
Any ``CharsetMatch`` object can be transformed to exploitable ``str`` variable. |
| 9 |
|
| 10 |
:: |
| 11 |
|
| 12 |
my_byte_str = 'Bсеки човек има право на образование.'.encode('cp1251') |
| 13 |
|
| 14 |
# Assign return value so we can fully exploit result |
| 15 |
result = from_bytes( |
| 16 |
my_byte_str |
| 17 |
).best() |
| 18 |
|
| 19 |
# This should print 'Bсеки човек има право на образование.' |
| 20 |
print(str(result)) |
| 21 |
|
| 22 |
|
| 23 |
Logging |
| 24 |
------- |
| 25 |
|
| 26 |
Prior to the version 2.0.11 you may encounter some unexpected logs in your streams. |
| 27 |
Something along the line of: |
| 28 |
|
| 29 |
:: |
| 30 |
|
| 31 |
... | WARNING | override steps (5) and chunk_size (512) as content does not fit (465 byte(s) given) parameters. |
| 32 |
... | INFO | ascii passed initial chaos probing. Mean measured chaos is 0.000000 % |
| 33 |
... | INFO | ascii should target any language(s) of ['Latin Based'] |
| 34 |
|
| 35 |
|
| 36 |
It is most likely because you altered the root getLogger instance. The package has its own logic behind logging and why |
| 37 |
it is useful. See https://docs.python.org/3/howto/logging.html to learn the basics. |
| 38 |
|
| 39 |
If you are looking to silence and/or reduce drastically the amount of logs, please upgrade to the latest version |
| 40 |
available for `charset-normalizer` using your package manager or by `pip install charset-normalizer -U`. |
| 41 |
|
| 42 |
The latest version will no longer produce any entry greater than `DEBUG`. |
| 43 |
On `DEBUG` only one entry will be observed and that is about the detection result. |
| 44 |
|
| 45 |
Then regarding the others log entries, they will be pushed as `Level 5`. Commonly known as TRACE level, but we do |
| 46 |
not register it globally. |
| 47 |
|
| 48 |
|
| 49 |
Detect binaries |
| 50 |
--------------- |
| 51 |
|
| 52 |
This package offers a neat way to detect files that can be considered as 'binaries' |
| 53 |
meaning that it is not likely to be a text-file. |
| 54 |
|
| 55 |
:: |
| 56 |
|
| 57 |
from charset_normalizer import is_binary |
| 58 |
|
| 59 |
# It can receive both a path or bytes or even a file pointer. |
| 60 |
result = is_binary("./my-file.ext") |
| 61 |
|
| 62 |
# This should print 'True' or 'False' |
| 63 |
print(result) |
| 64 |
|