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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This is a simple tool that converts CSV file into a JSON file.
# The first line of the input CSV file is used as the field names.
#
# Use 'OrderedDict' to keep the input fields order.
#
# Aaron LI
# 2015/06/11
#
from __future__ import print_function, division
__version__ = "0.1.0"
__date__ = "2015/06/11"
import sys
import argparse
import csv
import json
from collections import OrderedDict
def csv2json(csvfile, jsonfile=None):
"""
Convert CSV data to JSON data.
The first line of CSV data is used as the field names.
Return:
If jsonfile is None, then return a list of JSON dict.
"""
if not hasattr(csvfile, "read"):
csvfile = open(csvfile, "r")
if (jsonfile is not None) and (not hasattr(jsonfile, "write")):
jsonfile = open(jsonfile, "w")
csvdata = list(csv.reader(csvfile))
fieldnames = csvdata[0]
# use 'OrderedDict' to keep fields order
jsondata = [ OrderedDict(zip(fieldnames, row)) for row in csvdata[1:] ]
csvfile.close()
if jsonfile is None:
return jsondata
else:
# 'ensure_ascii=False' to support UTF-8
json.dump(jsondata, jsonfile, ensure_ascii=False, indent=4)
jsonfile.close()
def main():
# command line options & arguments
parser = argparse.ArgumentParser(
description="Simple CSV to JSON convertor")
parser.add_argument("-V", "--version", action="version",
version="%(prog)s " + "%s (%s)" % (__version__, __date__))
parser.add_argument("csvfile", help="Input CSV file")
parser.add_argument("jsonfile", nargs="?", default=sys.stdout,
help="Output JSON file (default stdout)")
args = parser.parse_args()
csv2json(args.csvfile, args.jsonfile)
if __name__ == "__main__":
main()
|