File: csvjson.rst

package info (click to toggle)
csvkit 2.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 40,664 kB
  • sloc: python: 4,924; perl: 1,000; makefile: 131; sql: 4
file content (140 lines) | stat: -rw-r--r-- 5,361 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
=======
csvjson
=======

Description
===========

Converts a CSV file into JSON or GeoJSON (depending on flags):

.. code-block:: none

   usage: csvjson [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
                  [-p ESCAPECHAR] [-z FIELD_SIZE_LIMIT] [-e ENCODING] [-L LOCALE]
                  [-S] [--blanks] [--null-value NULL_VALUES [NULL_VALUES ...]]
                  [--date-format DATE_FORMAT] [--datetime-format DATETIME_FORMAT]
                  [-H] [-K SKIP_LINES] [-v] [-l] [--zero] [-V] [-i INDENT]
                  [-k KEY] [--lat LAT] [--lon LON] [--type TYPE]
                  [--geometry GEOMETRY] [--crs CRS] [--no-bbox] [--stream]
                  [-y SNIFF_LIMIT] [-I]
                  [FILE]

   Convert a CSV file into JSON (or GeoJSON).

   positional arguments:
     FILE                  The CSV file to operate on. If omitted, will accept
                           input as piped data via STDIN.

   optional arguments:
     -h, --help            show this help message and exit
     -i INDENT, --indent INDENT
                           Indent the output JSON this many spaces. Disabled by
                           default.
     -k KEY, --key KEY     Output JSON as an object keyed by a given column, KEY,
                           rather than as an array. All column values must be
                           unique. If --lat and --lon are specified, this column
                           is used as the GeoJSON Feature ID.
     --lat LAT             A column index or name containing a latitude. Output
                           will be GeoJSON instead of JSON. Requires --lon.
     --lon LON             A column index or name containing a longitude. Output
                           will be GeoJSON instead of JSON. Requires --lat.
     --type TYPE           A column index or name containing a GeoJSON type.
                           Output will be GeoJSON instead of JSON. Requires --lat
                           and --lon.
     --geometry GEOMETRY   A column index or name containing a GeoJSON geometry.
                           Output will be GeoJSON instead of JSON. Requires --lat
                           and --lon.
     --crs CRS             A coordinate reference system string to be included
                           with GeoJSON output. Requires --lat and --lon.
     --no-bbox             Disable the calculation of a bounding box.
     --stream              Output JSON as a stream of newline-separated objects,
                           rather than an as an array.
     -y SNIFF_LIMIT, --snifflimit SNIFF_LIMIT
                           Limit CSV dialect sniffing to the specified number of
                           bytes. Specify "0" to disable sniffing entirely, or
                           "-1" to sniff the entire file.
     -I, --no-inference    Disable type inference (and --locale, --date-format,
                           --datetime-format, --no-leading-zeroes) when parsing
                           the input.

See also: :doc:`../common_arguments`.

Examples
========

Convert veteran's education dataset to JSON keyed by state abbreviation:

.. code-block:: console

   $ csvjson -k "State Abbreviate" -i 4 examples/realdata/FY09_EDU_Recipients_by_State.csv
   {
       "AL": {
           "State Name": "ALABAMA",
           "State Abbreviate": "AL",
           "Code": 1.0,
           "Montgomery GI Bill-Active Duty": 6718.0,
           "Montgomery GI Bill- Selective Reserve": 1728.0,
           "Dependents' Educational Assistance": 2703.0,
           "Reserve Educational Assistance Program": 1269.0,
           "Post-Vietnam Era Veteran's Educational Assistance Program": 8.0,
           "TOTAL": 12426.0,
           "j": null
       },
       "...": {
           "...": "..."
       }
   }

Convert locations of public art into GeoJSON:

.. code-block:: console

   $ csvjson --lat latitude --lon longitude --k slug --crs EPSG:4269 -i 4 examples/test_geo.csv
   {
       "type": "FeatureCollection", 
       "bbox": [
           -95.334619, 
           32.299076986939205, 
           -95.250699, 
           32.351434
       ], 
       "crs": {
           "type": "name", 
           "properties": {
               "name": "EPSG:4269"
           }
       }, 
       "features": [
           {
               "type": "Feature", 
               "id": "dcl", 
               "geometry": {
                   "type": "Point", 
                   "coordinates": [
                       -95.30181, 
                       32.35066
                   ]
               }, 
               "properties": {
                   "title": "Downtown Coffee Lounge", 
                   "artist": null, 
                   "description": "In addition to being the only coffee shop in downtown Tyler, DCL also features regular exhibitions of work by local artists.", 
                   "install_date": null, 
                   "address": "200 West Erwin Street", 
                   "type": "Gallery", 
                   "photo_url": null, 
                   "photo_credit": null, 
                   "last_seen_date": "2012-03-30"
               }
           }, 
           {
               "...": "..."
           }
       ], 
       "crs": {
           "type": "name", 
           "properties": {
               "name": "EPSG:4269"
           }
       }
   }