forked from shawnbot/csv2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2json.py
executable file
·63 lines (57 loc) · 2.42 KB
/
csv2json.py
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
#!/usr/local/bin/python
"""
A simple script for generating JSON/JavaScript from comma-separated (or
otherwise delimited) values.
by Shawn Allen <shawn at stamen dot com>
"""
import csv, simplejson
from StringIO import StringIO
# These are shorthands for delimiters that might be a pain to type or escape.
delimiter_map = {'tab': '\t',
'sc': ';',
'bar': '|'}
def csv2json(csv_file, delimiter=',', quotechar='"', indent=None, callback=None, variable=None, **csv_opts):
if delimiter_map.has_key(delimiter):
delimiter = delimiter_map.get(delimiter)
reader = csv.DictReader(csv_file, delimiter=delimiter, quotechar=quotechar or None, **csv_opts)
rows = [row for row in reader]
if hasattr(indent, 'isdigit') and indent.isdigit():
indent = ' ' * int(indent)
out = StringIO()
if callback:
out.write('%s(' % callback);
elif variable:
out.write('var %s = ' % variable)
simplejson.dump(rows, out, indent=indent)
if callback:
out.write(');');
elif variable:
out.write(';')
return out.getvalue()
if __name__ == '__main__':
import sys
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-F', '--field-separator', dest='fs', default=',',
help='The CSV file field separator, default: %default')
parser.add_option('-q', '--field-quote', dest='fq', default='"',
help='The CSV file field quote character, default: %default')
parser.add_option('-i', '--indent', dest='indent', default=None,
help='The string with which to indent the output GeoJSON, '
'defaults to none.')
parser.add_option('-p', '--callback', dest='callback', default=None,
help='The JSON-P callback function name.')
parser.add_option('-v', '--variable', dest='var', default=None,
help='If provided, the output becomes a JavaScript statement'
' which assigns the JSON structure to a variable of the same'
' name.')
options, args = parser.parse_args()
close = False
if len(args) > 0 and args[0] != '-':
csv_file = open(args.pop(0), 'r')
close = True
else:
csv_file = sys.stdin
print csv2json(csv_file, options.fs, options.fq, options.indent, options.callback, options.var)
if close:
csv_file.close()