| 1 |
============================== |
| 2 |
Going elsewhere with your data |
| 3 |
============================== |
| 4 |
|
| 5 |
csvjson: going online |
| 6 |
===================== |
| 7 |
|
| 8 |
Very frequently one of the last steps in any data analysis is to get the data onto the web for display as a table, map or chart. CSV is rarely the ideal format for this. More often than not what you want is JSON and that's where :doc:`/scripts/csvjson` comes in. :doc:`/scripts/csvjson` takes an input CSV and outputs neatly formatted JSON. For the sake of illustration, let's use :doc:`/scripts/csvcut` and :doc:`/scripts/csvgrep` to convert just a small slice of our data: |
| 9 |
|
| 10 |
.. code-block:: bash |
| 11 |
|
| 12 |
csvcut -c county,item_name data.csv | csvgrep -c county -m "GREELEY" | csvjson --indent 4 |
| 13 |
|
| 14 |
.. code-block:: json |
| 15 |
|
| 16 |
[ |
| 17 |
{ |
| 18 |
"county": "GREELEY", |
| 19 |
"item_name": "RIFLE,7.62 MILLIMETER" |
| 20 |
}, |
| 21 |
{ |
| 22 |
"county": "GREELEY", |
| 23 |
"item_name": "RIFLE,7.62 MILLIMETER" |
| 24 |
}, |
| 25 |
{ |
| 26 |
"county": "GREELEY", |
| 27 |
"item_name": "RIFLE,7.62 MILLIMETER" |
| 28 |
} |
| 29 |
] |
| 30 |
|
| 31 |
A common usage of turning a CSV into a JSON file is for usage as a lookup table in the browser. This can be illustrated with the ACS data we looked at earlier, which contains a unique ``fips`` code for each county: |
| 32 |
|
| 33 |
.. code-block:: bash |
| 34 |
|
| 35 |
csvjson --indent 4 --key fips acs2012_5yr_population.csv | head |
| 36 |
|
| 37 |
.. code-block:: json |
| 38 |
|
| 39 |
{ |
| 40 |
"31001": { |
| 41 |
"fips": "31001", |
| 42 |
"name": "Adams County, NE", |
| 43 |
"total_population": "31299", |
| 44 |
"margin_of_error": "0" |
| 45 |
}, |
| 46 |
"31003": { |
| 47 |
"fips": "31003", |
| 48 |
"name": "Antelope County, NE", |
| 49 |
"...": "..." |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
For making maps, :doc:`/scripts/csvjson` can also output GeoJSON, see its :doc:`/scripts/csvjson` for more details. |
| 54 |
|
| 55 |
csvpy: going into code |
| 56 |
====================== |
| 57 |
|
| 58 |
For the programmers out there, the command line is rarely as functional as just writing a little bit of code. :doc:`/scripts/csvpy` exists just to make a programmer's life easier. Invoking it simply launches a Python interactive terminal, with the data preloaded into a CSV reader: |
| 59 |
|
| 60 |
.. code-block:: console |
| 61 |
|
| 62 |
$ csvpy data.csv |
| 63 |
Welcome! "data.csv" has been loaded in a reader object named "reader". |
| 64 |
>>> print(len(list(reader))) |
| 65 |
1037 |
| 66 |
>>> quit() |
| 67 |
|
| 68 |
In addition to being a time-saver, because this uses agate, the reader is Unicode aware. |
| 69 |
|
| 70 |
csvformat: for legacy systems |
| 71 |
============================= |
| 72 |
|
| 73 |
It is a foundational principle of csvkit that it always outputs cleanly formatted CSV data. None of the normal csvkit tools can be forced to produce pipe or tab-delimited output, despite these being common formats. This principle is what allows the csvkit tools to chain together so easily and hopefully also reduces the amount of crummy, non-standard CSV files in the world. However, sometimes a legacy system just has to have a pipe-delimited file and it would be crazy to make you use another tool to create it. That's why we've got :doc:`/scripts/csvformat`. |
| 74 |
|
| 75 |
Pipe-delimited: |
| 76 |
|
| 77 |
.. code-block:: bash |
| 78 |
|
| 79 |
csvformat -D \| data.csv |
| 80 |
|
| 81 |
Tab-delimited: |
| 82 |
|
| 83 |
.. code-block:: bash |
| 84 |
|
| 85 |
csvformat -T data.csv |
| 86 |
|
| 87 |
Quote every cell: |
| 88 |
|
| 89 |
.. code-block:: bash |
| 90 |
|
| 91 |
csvformat -U 1 data.csv |
| 92 |
|
| 93 |
Ampersand-delimited, dollar-signs for quotes, quote all strings, and asterisk for line endings: |
| 94 |
|
| 95 |
.. code-block:: bash |
| 96 |
|
| 97 |
csvformat -D \& -Q \$ -U 2 -M \* data.csv |
| 98 |
|
| 99 |
You get the picture. |
| 100 |
|
| 101 |
Summing up |
| 102 |
========== |
| 103 |
|
| 104 |
Thus concludes the csvkit tutorial. At this point, I hope, you have a sense a breadth of possibilities these tools open up with a relatively small number of command-line tools. Of course, this tutorial has only scratched the surface of the available options, so remember to check the :doc:`/cli` documentation for each tool as well. |
| 105 |
|
| 106 |
So armed, go forth and expand the empire of the king of tabular file formats. |