| 1 |
Command Line Interface |
| 2 |
====================== |
| 3 |
|
| 4 |
charset-normalizer ship with a CLI that should be available as `normalizer`. |
| 5 |
This is a great tool to fully exploit the detector capabilities without having to write Python code. |
| 6 |
|
| 7 |
Possible use cases: |
| 8 |
|
| 9 |
#. Quickly discover probable originating charset from a file. |
| 10 |
#. I want to quickly convert a non Unicode file to Unicode. |
| 11 |
#. Debug the charset-detector. |
| 12 |
|
| 13 |
Down below, we will guide you through some basic examples. |
| 14 |
|
| 15 |
Arguments |
| 16 |
--------- |
| 17 |
|
| 18 |
You may simply invoke `normalizer -h` (with the h(elp) flag) to understand the basics. |
| 19 |
|
| 20 |
:: |
| 21 |
|
| 22 |
usage: normalizer [-h] [-v] [-a] [-n] [-m] [-r] [-f] [-t THRESHOLD] |
| 23 |
file [file ...] |
| 24 |
|
| 25 |
The Real First Universal Charset Detector. Discover originating encoding used |
| 26 |
on text file. Normalize text to unicode. |
| 27 |
|
| 28 |
positional arguments: |
| 29 |
files File(s) to be analysed |
| 30 |
|
| 31 |
optional arguments: |
| 32 |
-h, --help show this help message and exit |
| 33 |
-v, --verbose Display complementary information about file if any. |
| 34 |
Stdout will contain logs about the detection process. |
| 35 |
-a, --with-alternative |
| 36 |
Output complementary possibilities if any. Top-level |
| 37 |
JSON WILL be a list. |
| 38 |
-n, --normalize Permit to normalize input file. If not set, program |
| 39 |
does not write anything. |
| 40 |
-m, --minimal Only output the charset detected to STDOUT. Disabling |
| 41 |
JSON output. |
| 42 |
-r, --replace Replace file when trying to normalize it instead of |
| 43 |
creating a new one. |
| 44 |
-f, --force Replace file without asking if you are sure, use this |
| 45 |
flag with caution. |
| 46 |
-t THRESHOLD, --threshold THRESHOLD |
| 47 |
Define a custom maximum amount of chaos allowed in |
| 48 |
decoded content. 0. <= chaos <= 1. |
| 49 |
--version Show version information and exit. |
| 50 |
|
| 51 |
.. code:: bash |
| 52 |
|
| 53 |
normalizer ./data/sample.1.fr.srt |
| 54 |
|
| 55 |
You may also run the command line interface using: |
| 56 |
|
| 57 |
.. code:: bash |
| 58 |
|
| 59 |
python -m charset_normalizer ./data/sample.1.fr.srt |
| 60 |
|
| 61 |
|
| 62 |
Main JSON Output |
| 63 |
---------------- |
| 64 |
|
| 65 |
🎉 Since version 1.4.0 the CLI produce easily usable stdout result in |
| 66 |
JSON format. |
| 67 |
|
| 68 |
.. code:: json |
| 69 |
|
| 70 |
{ |
| 71 |
"path": "/home/default/projects/charset_normalizer/data/sample.1.fr.srt", |
| 72 |
"encoding": "cp1252", |
| 73 |
"encoding_aliases": [ |
| 74 |
"1252", |
| 75 |
"windows_1252" |
| 76 |
], |
| 77 |
"alternative_encodings": [ |
| 78 |
"cp1254", |
| 79 |
"cp1256", |
| 80 |
"cp1258", |
| 81 |
"iso8859_14", |
| 82 |
"iso8859_15", |
| 83 |
"iso8859_16", |
| 84 |
"iso8859_3", |
| 85 |
"iso8859_9", |
| 86 |
"latin_1", |
| 87 |
"mbcs" |
| 88 |
], |
| 89 |
"language": "French", |
| 90 |
"alphabets": [ |
| 91 |
"Basic Latin", |
| 92 |
"Latin-1 Supplement" |
| 93 |
], |
| 94 |
"has_sig_or_bom": false, |
| 95 |
"chaos": 0.149, |
| 96 |
"coherence": 97.152, |
| 97 |
"unicode_path": null, |
| 98 |
"is_preferred": true |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
I recommend the `jq` command line tool to easily parse and exploit specific data from the produced JSON. |
| 103 |
|
| 104 |
Multiple File Input |
| 105 |
------------------- |
| 106 |
|
| 107 |
It is possible to give multiple files to the CLI. It will produce a list instead of an object at the top level. |
| 108 |
When using the `-m` (minimal output) it will rather print one result (encoding) per line. |
| 109 |
|
| 110 |
Unicode Conversion |
| 111 |
------------------ |
| 112 |
|
| 113 |
If you desire to convert any file to Unicode you will need to append the flag `-n`. It will produce another file, |
| 114 |
it won't replace it by default. |
| 115 |
|
| 116 |
The newly created file path will be declared in `unicode_path` (JSON output). |