Debian Codegraph @

docs/scripts/in2csv.rst

raw
1 ======
2 in2csv
3 ======
4
5 Description
6 ===========
7
8 Converts various tabular data formats into CSV.
9
10 Converting fixed width requires that you provide a schema file with the "-s" option. The schema file should have the following format:
11
12 .. code-block:: none
13
14 column,start,length
15 name,0,30
16 birthday,30,10
17 age,40,3
18
19 The header line is required though the columns may be in any order:
20
21 .. code-block:: none
22
23 usage: in2csv [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
24 [-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-L LOCALE]
25 [-S] [--blanks] [--null-value NULL_VALUES [NULL_VALUES ...]]
26 [--date-format DATE_FORMAT] [--datetime-format DATETIME_FORMAT]
27 [-H] [-K SKIP_LINES] [-v] [-l] [--zero] [-V]
28 [-f {csv,dbf,fixed,geojson,json,ndjson,xls,xlsx}] [-s SCHEMA]
29 [-k KEY] [-n] [--sheet SHEET] [--write-sheets WRITE_SHEETS]
30 [--use-sheet-names] [--reset-dimensions]
31 [--encoding-xls ENCODING_XLS] [-y SNIFF_LIMIT] [-I]
32 [FILE]
33
34 Convert common, but less awesome, tabular data formats to CSV.
35
36 positional arguments:
37 FILE The CSV file to operate on. If omitted, will accept
38 input as piped data via STDIN.
39
40 optional arguments:
41 -h, --help show this help message and exit
42 -f {csv,dbf,fixed,geojson,json,ndjson,xls,xlsx}, --format {csv,dbf,fixed,geojson,json,ndjson,xls,xlsx}
43 The format of the input file. If not specified will be
44 inferred from the file type.
45 -s SCHEMA, --schema SCHEMA
46 Specify a CSV-formatted schema file for converting
47 fixed-width files. See web documentation.
48 -k KEY, --key KEY Specify a top-level key to look within for a list of
49 objects to be converted when processing JSON.
50 -n, --names Display sheet names from the input Excel file.
51 --sheet SHEET The name of the Excel sheet to operate on.
52 --write-sheets WRITE_SHEETS
53 The names of the Excel sheets to write to files, or
54 "-" to write all sheets.
55 --use-sheet-names Use the sheet names as file names when --write-sheets
56 is set.
57 --reset-dimensions Ignore the sheet dimensions provided by the XLSX file.
58 --encoding-xls ENCODING_XLS
59 Specify the encoding of the input XLS file.
60 -y SNIFF_LIMIT, --snifflimit SNIFF_LIMIT
61 Limit CSV dialect sniffing to the specified number of
62 bytes. Specify "0" to disable sniffing entirely, or
63 "-1" to sniff the entire file.
64 -I, --no-inference Disable type inference (and --locale, --date-format,
65 --datetime-format, --no-leading-zeroes) when parsing
66 CSV input.
67
68 Some command-line flags only pertain to specific input formats.
69
70 See also: :doc:`../common_arguments`.
71
72 .. note::
73
74 The "ndjson" format refers to "newline delimited JSON", as used by many streaming APIs.
75
76 .. note::
77
78 If an XLS looks identical to an XLSX when viewed in Excel, they may not be identical as CSV. For example, XLSX has an integer type, but XLS doesn't. Numbers that look like integers from an XLS will have decimals in CSV, but those from an XLSX won't.
79
80 .. note::
81
82 To convert from HTML, consider `messytables <https://messytables.readthedocs.io/>`_.
83
84 Examples
85 ========
86
87 Convert the 2000 census geo headers file from fixed-width to CSV and from latin-1 encoding to utf8:
88
89 .. code-block:: bash
90
91 in2csv -e iso-8859-1 -f fixed -s examples/realdata/census_2000/census2000_geo_schema.csv examples/realdata/census_2000/usgeo_excerpt.upl
92
93 .. note::
94
95 A library of fixed-width schemas is maintained in the ``ffs`` project:
96
97 https://github.com/wireservice/ffs
98
99 Convert an Excel .xls file:
100
101 .. code-block:: bash
102
103 in2csv examples/test.xls
104
105 Standardize the formatting of a CSV file (quoting, line endings, etc.):
106
107 .. code-block:: bash
108
109 in2csv examples/realdata/FY09_EDU_Recipients_by_State.csv
110
111 Fetch csvkit's open issues from the GitHub API, convert the JSON response into a CSV and write it to a file:
112
113 .. code-block:: bash
114
115 curl https://api.github.com/repos/wireservice/csvkit/issues?state=open | in2csv -f json -v
116
117 Convert a DBase DBF file to an equivalent CSV:
118
119 .. code-block:: bash
120
121 in2csv examples/testdbf.dbf
122
123 This tool names unnamed headers. To avoid that behavior, run:
124
125 .. code-block:: bash
126
127 in2csv --no-header-row examples/test.xlsx | tail -n +2
128
129 Troubleshooting
130 ===============
131
132 If an error like the following occurs when providing an input file in CSV or Excel format:
133
134 .. code-block:: none
135
136 ValueError: Row 0 has 11 values, but Table only has 1 columns.
137
138 Then the input file might have initial rows before the header and data rows. You can skip such rows with :code:`--skip-lines` (:code:`-K`):
139
140 .. code-block:: bash
141
142 in2csv --skip-lines 3 examples/test_skip_lines.csv
143
144 If an XLSX file yields too few rows or too few columns, then the application that created the file might have `incorrectly set the worksheet's dimensions <https://openpyxl.readthedocs.io/en/stable/optimized.html#worksheet-dimensions>`__. Try again with the :code:`--reset-dimensions` option.