-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_to_sql.py
More file actions
47 lines (42 loc) · 1.09 KB
/
parse_to_sql.py
File metadata and controls
47 lines (42 loc) · 1.09 KB
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
import json
import codecs
import datetime
from pprint import pprint
with open('investor.json') as data:
sql = file('investor.sql', 'w');
index = 0
while True:
line = data.readline()
if len(line) == 0:
break
INSERT_TEMPLATE = 'INSERT INTO investor(`id`'
index = index + 1
# if index == 900:
# break
line = json.loads(line, "utf-8")
values = []
for key in line.keys():
value = line[key]
if key in ['title']:
value.pop(0)
for idx, item in enumerate(value):
item = item.strip()
if item.endswith(','):
item = item[:-2]
value[idx] = item
if type(value) is list:
value = ','.join(value)
if key in ['avatar']:
value = value[(value.rfind('/') + 1):]
if key in ['url']:
identifier = int(value[(value.rfind('/') + 1):])
if key not in ['url', 'images']:
values.append(value)
INSERT_TEMPLATE += ', `' + key + '`'
INSERT_TEMPLATE += ') values(' + `identifier`
for value in values:
INSERT_TEMPLATE += ", '" + value.strip() + "'"
INSERT_TEMPLATE += ');\n'
sql.write(INSERT_TEMPLATE.encode('utf-8'))
sql.close()
data.close()