| 1 |
Installation |
| 2 |
============ |
| 3 |
|
| 4 |
This installs a package that can be used from Python (``import charset_normalizer``). |
| 5 |
|
| 6 |
To install for all users on the system, administrator rights (root) may be required. |
| 7 |
|
| 8 |
Using PIP |
| 9 |
--------- |
| 10 |
Charset Normalizer can be installed from pip:: |
| 11 |
|
| 12 |
pip install charset-normalizer |
| 13 |
|
| 14 |
You may retrieve the latest unicodedata backport as follow:: |
| 15 |
|
| 16 |
pip install charset-normalizer[unicode_backport] |
| 17 |
|
| 18 |
From git via master |
| 19 |
----------------------- |
| 20 |
You can install from dev-master branch using git:: |
| 21 |
|
| 22 |
git clone https://github.com/Ousret/charset_normalizer.git |
| 23 |
cd charset_normalizer/ |
| 24 |
python setup.py install |
| 25 |
|
| 26 |
Basic Usage |
| 27 |
=========== |
| 28 |
|
| 29 |
The new way |
| 30 |
----------- |
| 31 |
|
| 32 |
You may want to get right to it. :: |
| 33 |
|
| 34 |
from charset_normalizer import from_bytes, from_path |
| 35 |
|
| 36 |
# This is going to print out your sequence once properly decoded |
| 37 |
print( |
| 38 |
str( |
| 39 |
from_bytes( |
| 40 |
my_byte_str |
| 41 |
).best() |
| 42 |
) |
| 43 |
) |
| 44 |
|
| 45 |
# You could also want the same from a file |
| 46 |
print( |
| 47 |
str( |
| 48 |
from_path( |
| 49 |
'./data/sample.1.ar.srt' |
| 50 |
).best() |
| 51 |
) |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
Backward compatibility |
| 56 |
---------------------- |
| 57 |
|
| 58 |
If you were used to python chardet, we are providing the very same ``detect()`` method as chardet. |
| 59 |
This function is mostly backward-compatible with Chardet. The migration should be painless. |
| 60 |
|
| 61 |
:: |
| 62 |
|
| 63 |
from charset_normalizer import detect |
| 64 |
|
| 65 |
# This will behave exactly the same as python chardet |
| 66 |
result = detect(my_byte_str) |
| 67 |
|
| 68 |
if result['encoding'] is not None: |
| 69 |
print('got', result['encoding'], 'as detected encoding') |
| 70 |
|
| 71 |
|
| 72 |
You may upgrade your code with ease. |
| 73 |
CTRL + R ``from chardet import detect`` to ``from charset_normalizer import detect``. |