Debian Codegraph @

docs/scripts/csvclean.rst

raw
1 ========
2 csvclean
3 ========
4
5 Description
6 ===========
7
8 Reports and fixes common errors in a CSV file.
9
10 Checks
11 ------
12
13 - Reports rows that have a different number of columns than the header row, if the :code:`--length-mismatch` option is set.
14 - Reports columns that are empty, if the :code:`--empty-columns` option is set.
15
16 .. tip::
17
18 Enable all checks with :code:`--enable-all-checks` (:code:`-a`).
19
20 Fixes
21 -----
22
23 - If a CSV has unquoted cells that contain line breaks, like:
24
25 .. code-block:: none
26
27 id,address,country
28 1,1 Main St
29 Springfield,US
30 2,123 Acadia Avenue
31 London,GB
32
33 Use :code:`--join-short-rows` to attempt to correct the errors by merging short rows into a single row:
34
35 .. code-block:: none
36
37 id,address,country
38 1,"1 Main St
39 Springfield",US
40 2,"123 Acadia Avenue
41 London",GB
42
43 To change the string used to join the lines, use :code:`--separator`. For example, with :code:`--separator ", "`:
44
45 .. code-block:: none
46
47 id,address,country
48 1,"1 Main St, Springfield",US
49 2,"123 Acadia Avenue, London",GB
50
51 - If a CSV has missing delimiters, like:
52
53 .. code-block:: none
54
55 id,name,country
56 1,Alice
57 2,Bob,CA
58
59 You can add the missing delimiters with :code:`--fill-short-rows`:
60
61 .. code-block:: none
62
63 id,name,country
64 1,Alice,
65 2,Bob,CA
66
67 .. tip::
68
69 :doc:`csvcut` without options also adds missing delimiters!
70
71 To change the value used to fill short rows, use :code:`--fillvalue`. For example, with :code:`--fillvalue "US"`:
72
73 .. code-block:: none
74
75 id,name,country
76 1,Alice,US
77 2,Bob,CA
78
79 .. seealso::
80
81 :code:`--header-normalize-space` under :ref:`csvclean-usage`.
82
83 .. note::
84
85 Every csvkit tool does the following:
86
87 - Removes optional quote characters, unless the `--quoting` (`-u`) option is set to change this behavior
88 - Changes the field delimiter to a comma, if the input delimiter is set with the `--delimiter` (`-d`) or `--tabs` (`-t`) options
89 - Changes the record delimiter to a line feed (LF or ``\n``)
90 - Changes the quote character to a double-quotation mark, if the character is set with the `--quotechar` (`-q`) option
91 - Changes the character encoding to UTF-8, if the input encoding is set with the `--encoding` (`-e`) option
92
93 Output
94 ------
95
96 :code:`csvclean` attempts to make the selected fixes. Then:
97
98 - If the :code:`--omit-error-rows` option is set, **only** rows that pass the selected checks are written to standard output. If not, **all** rows are written to standard output.
99 - If any checks are enabled, **error** rows along with line numbers and descriptions are written to standard error. If there are error rows, the exit code is 1.
100
101 .. _csvclean-usage:
102
103 Usage
104 -----
105
106 .. code-block:: none
107
108 usage: csvclean [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
109 [-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-S] [-H]
110 [-K SKIP_LINES] [-v] [-l] [--zero] [-V]
111 [FILE]
112
113 Fix common errors in a CSV file.
114
115 positional arguments:
116 FILE The CSV file to operate on. If omitted, will accept
117 input as piped data via STDIN.
118
119 optional arguments:
120 -h, --help show this help message and exit
121 --length-mismatch Report data rows that are shorter or longer than the
122 header row.
123 --empty-columns Report empty columns as errors.
124 -a, --enable-all-checks
125 Enable all error reporting.
126 --omit-error-rows Omit data rows that contain errors, from standard
127 output.
128 --label LABEL Add a "label" column to standard error. Useful in
129 automated workflows. Use "-" to default to the input
130 filename.
131 --header-normalize-space
132 Strip leading and trailing whitespace and replace
133 sequences of whitespace characters by a single space
134 in the header.
135 --join-short-rows Merges short rows into a single row.
136 --separator SEPARATOR
137 The string with which to join short rows. Defaults to
138 a newline.
139 --fill-short-rows Fill short rows with the missing cells.
140 --fillvalue FILLVALUE
141 The value with which to fill short rows. Defaults to
142 none.
143
144
145 See also: :doc:`../common_arguments`.
146
147 Examples
148 ========
149
150 Process a file with data rows that are shorter and longer than the header row, and omit those rows:
151
152 .. code-block:: console
153
154 $ csvclean --length-mismatch --omit-error-rows examples/bad.csv 2> errors.csv
155 column_a,column_b,column_c
156 0,mixed types.... uh oh,17
157
158 .. code-block:: console
159
160 $ cat errors.csv
161 line_number,msg,column_a,column_b,column_c
162 1,"Expected 3 columns, found 4 columns",1,27,,I'm too long!
163 2,"Expected 3 columns, found 2 columns",,I'm too short!
164
165 .. note::
166
167 If any data rows are longer than the header row, you need to add columns manually: for example, by adding one or more delimiters (``,``) to the end of the header row. :code:`csvclean` can't do this, because it is designed to work with standard input, and correcting an error at the start of the CSV data based on an observation later in the CSV data would require holding all the CSV data in memory – which is not an option for large files.
168
169 Process a file with empty columns:
170
171 .. code-block:: console
172
173 $ csvclean --empty-columns examples/test_empty_columns.csv 2> errors.csv
174 a,b,c,,
175 a,,,,
176 ,,c,,
177 ,,,,
178
179 .. code-block:: console
180 :emphasize-lines: 3
181
182 $ cat errors.csv
183 line_number,msg,a,b,c,,
184 1,"Empty columns named 'b', '', ''! Try: csvcut -C 2,4,5",,,,,
185
186 Then, use :doc:`csvcut` to exclude the empty columns:
187
188 .. code-block:: console
189
190 $ csvcut -C 2,4,5 examples/test_empty_columns.csv
191 a,c
192 a,
193 ,c
194 ,
195
196 Check whether any errors found:
197
198 .. code-block:: console
199
200 $ if [ csvclean -a examples/bad.csv ]; then echo "my message"; fi
201 my message
202
203 Or:
204
205 .. code-block:: console
206
207 $ [ csvclean -a examples/bad.csv ] && echo "my message"
208 my message
209
210 Or:
211
212 .. code-block:: console
213
214 $ csvclean -a examples/bad.csv >/dev/null 2>&1
215 $ echo $?
216 1
217
218 Count the number of errors found:
219
220 .. code-block:: console
221
222 $ csvclean -a examples/bad.csv 2>&1 >/dev/null | csvstat --count
223 2
224
225 View only the errors found:
226
227 .. code-block:: console
228
229 $ csvclean -a examples/bad.csv 2>&1 >/dev/null
230 line_number,msg,column_a,column_b,column_c
231 1,"Expected 3 columns, found 4 columns",1,27,,I'm too long!
232 2,"Expected 3 columns, found 2 columns",,I'm too short!
233
234 To change the line ending from line feed (LF or ``\n``) to carriage return and line feed (CRLF or ``\r\n``) use:
235
236 .. code-block:: bash
237
238 csvformat -M $'\r\n' examples/dummy.csv