-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcsv_to_json.py
121 lines (103 loc) · 3.82 KB
/
csv_to_json.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
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
# -*- coding: utf-8 -*-
import argparse
import inspect
import math
import os
from pprint import pprint
import sys
from lib.collection_utils import *
from lib.io_utils import *
from lib.math_utils import *
# input
parser = argparse.ArgumentParser()
parser.add_argument('-in', dest="INPUT_FILE", default="tmp/samples.csv", help="Input csv file")
parser.add_argument('-props', dest="PROPS", default="", help="Comma-separated list of properties to output; leave empty for all")
parser.add_argument('-groups', dest="GROUPS", default="", help="Comma-separated list of groups to output")
parser.add_argument('-lists', dest="LISTS", default="", help="Comma-separated list of lists to output")
parser.add_argument('-filter', dest="FILTER", default="", help="Filter string")
parser.add_argument('-sort', dest="SORT", default="", help="Sort string")
parser.add_argument('-replace', dest="REPLACE_STRING", default="", help="Query string of keys to rename, e.g. keyFind=keyReplace&key2Find=key2Replace")
parser.add_argument('-limit', dest="LIMIT", default=-1, type=int, help="Limit the number of results; -1 for all")
parser.add_argument('-light', dest="LIGHT", action="store_true", help="Output the data in a 'light' format")
parser.add_argument('-out', dest="OUTPUT_FILE", default="output/samples.json", help="Output json file")
a = parser.parse_args()
# Parse arguments
props = [p for p in a.PROPS.strip().split(",")]
props = [p for p in props if len(p) > 0]
# Read files
fieldNames, rows = readCsv(a.INPUT_FILE)
if len(props) < 1:
props = fieldNames
rowCount = len(rows)
for i, row in enumerate(rows):
if "id" in row:
rows[i]["id"] = str(row["id"])
if "title" in row:
rows[i]["title"] = str(row["title"])
if len(a.FILTER) > 0:
rows = filterByQueryString(rows, a.FILTER)
rowCount = len(rows)
print("%s rows after filtering" % rowCount)
if len(a.SORT) > 0:
rows = sortByQueryString(rows, a.SORT)
rowCount = len(rows)
print("%s rows after sorting" % rowCount)
replaceKeys = {}
if len(a.REPLACE_STRING) > 0:
replaceKeys = parseQueryString(a.REPLACE_STRING, doParseNumbers=False)
if a.LIMIT > 0 and len(rows) > a.LIMIT:
rows = rows[:a.LIMIT]
rowCount = len(rows)
print("%s rows after limiting" % rowCount)
GROUPS = [p for p in a.GROUPS.strip().split(",")] if len(a.GROUPS) > 0 else []
groups = None
if len(GROUPS) > 0:
groups = {}
for groupKey in GROUPS:
groupKeyNew = groupKey
if groupKey in replaceKeys:
groupKeyNew = replaceKeys[groupKey]
groups[groupKeyNew] = sorted(unique([r[groupKey] for r in rows]))
LISTS = [p for p in a.LISTS.strip().split(",")] if len(a.LISTS) > 0 else []
for i, r in enumerate(rows):
for listKey in LISTS:
rows[i][listKey] = r[listKey].split(" | ")
if len(LISTS) > 0:
if groups is None:
groups = {}
for groupKey in LISTS:
groupKeyNew = groupKey
if groupKey in replaceKeys:
groupKeyNew = replaceKeys[groupKey]
groups[groupKeyNew] = sorted(unique(flattenList([r[groupKey] for r in rows])))
items = []
for r in rows:
item = {}
for p in props:
pnew = p
if p in replaceKeys:
pnew = replaceKeys[p]
if p in GROUPS:
item[pnew] = groups[pnew].index(r[p])
elif p in LISTS:
item[pnew] = [groups[pnew].index(value) for value in r[p]]
else:
item[pnew] = r[p]
items.append(item)
for i, p in enumerate(props):
if p in replaceKeys:
props[i] = replaceKeys[p]
jsonOut = {}
if a.LIGHT:
jsonOut["itemHeadings"] = props
jrows = []
for r in items:
jrows.append([r[p] for p in props])
jsonOut["items"] = jrows
else:
jsonOut["items"] = items
if groups is not None:
jsonOut["groups"] = groups
jsonOut["lists"] = LISTS
makeDirectories(a.OUTPUT_FILE)
writeJSON(a.OUTPUT_FILE, jsonOut)