forked from wireservice/csvkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvjson
executable file
·34 lines (25 loc) · 816 Bytes
/
csvjson
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
#!/usr/bin/env python
import json
import sys
from csvkit import CSVKitReader
from csvkit.cli import CSVKitUtility
class CSVJSON(CSVKitUtility):
"""
Convert CSV into JSON.
"""
def add_arguments(self):
self.argparser.add_argument('-i', '--indent', dest='indent', type=int, default=None,
help='Indent the output JSON this many spaces. Disabled by default.')
def main(self):
"""
Convert CSV to JSON.
"""
rows = CSVKitReader(self.args.file, **self.reader_kwargs)
column_names = rows.next()
output = []
for row in rows:
output.append(dict(zip(column_names, row)))
sys.stdout.write(json.dumps(output, indent=self.args.indent))
if __name__ == "__main__":
utility = CSVJSON()
utility.main()