-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand.py
159 lines (129 loc) · 3.65 KB
/
expand.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import csv
import argparse
import sys
import json
data = []
maxInt = sys.maxsize
decrement = True
while decrement:
try:
csv.field_size_limit(maxInt)
decrement = False
except OverflowError:
maxInt = int(maxInt / 2)
def read_data(filename):
global data
csv_file = open(filename, 'r', encoding='utf-8')
csv_reader = csv.reader(csv_file)
for line in csv_reader:
data.append(line)
csv_file.close()
def header_row():
global data
header_row = 0
while header_row < len(data) and data[header_row] != []:
header_row += 1
header_row += 1
if header_row >= len(data):
header_row = 0
return header_row
def json_keys(cell):
try:
int(cell)
return None
except ValueError:
pass
try:
float(cell)
return None
except ValueError:
pass
try:
return list(json.loads(cell)[0].keys())
except ValueError:
return None
def find_json_columns(range_start, range_end):
global data, header_row, skip_columns
json_columns = {}
for column in range(range_start, range_end):
if data[header_row][column] in skip_columns:
continue
row = header_row + 1
while row < len(data):
try:
if data[row][column] == None or data[row][column] == '':
row += 1
else:
break
except:
row += 1
if row == len(data):
continue
keys = json_keys(data[row][column])
if keys != None:
json_columns[column] = keys
return json_columns
def fix_json_columns(range_start, range_end):
global data, header_row
json_columns = find_json_columns(range_start, range_end)
cols_added = 0
for column in json_columns.keys():
adj_col = column + cols_added
header_keys = []
for header in json_columns[column]:
header_keys.append(data[header_row][adj_col] + '_' + header)
data[header_row] = data[header_row][:adj_col] + header_keys + data[header_row][adj_col + 1:]
new_data = []
for row in range(header_row + 1, len(data)):
if data[row][adj_col] == None or data[row][adj_col] == '':
empty_cells = [None] * len(json_columns[column])
new_data.append(data[row][:adj_col] + empty_cells + data[row][adj_col + 1:])
continue
cell_data = json.loads(data[row][adj_col])
for item in cell_data:
new_row = data[row][:adj_col]
for key in json_columns[column]:
if type(item[key]) == list:
new_row += [json.dumps(item[key])]
else:
new_row += [item[key]]
new_row += data[row][adj_col + 1:]
new_data.append(new_row)
data = data[:header_row + 1] + new_data
cols_added += fix_json_columns(column, column + len(json_columns[column]))
cols_added += len(json_columns[column]) - 1
return cols_added
def new_filename(filename):
name_parts = filename.split('.')
if len(name_parts) == 1:
name_parts[0] += '_expanded'
else:
name_parts[-2] += '_expanded'
return '.'.join(name_parts)
def write_data(new_filename):
global data
expanded = open(new_filename, 'w', encoding='utf-8', newline='')
csv_writer = csv.writer(expanded)
for line in data:
csv_writer.writerow(line)
expanded.close()
def expand(filename):
global data, header_row
print('Reading data from %s...' % (filename))
read_data(filename)
header_row = header_row()
print('Expanding columns with json data...')
fix_json_columns(0, len(data[header_row]))
new_file = new_filename(filename)
print('Writing data to %s...' % (new_file))
write_data(new_file)
print('Done!')
parser = argparse.ArgumentParser()
parser.add_argument('filename', help='The CSV file to process', nargs=1)
parser.add_argument('-x','--skip-column', help='Specify any columns you not want to expand.', nargs='*')
args = parser.parse_args()
filename = args.filename[0]
skip_columns = args.skip_column
if skip_columns == None:
skip_columns = []
expand(filename)