-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_data.py
82 lines (70 loc) · 1.81 KB
/
load_data.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
from load import *
import json
def JSON_Read():
# read JSON file data
data = None
with open('data/data.json', 'r') as f:
data = json.load(f)
return data
# data from list
def get_enterprises(data):
# name of enterprises
enterprises = []
for i in data:
# print(i['name'])
if i['name'] not in enterprises:
enterprises.append(i['name'])
return enterprises
def get_tools(data):
tools = []
for i in data:
for j in i['tools']:
for k in i['tools'][j]:
if k not in tools:
tools.append(k)
return tools
def get_categories(data):
groups = []
for i in data:
for j in i['tools']:
if j not in groups:
groups.append(j)
return groups
def enterprise_tool(data):
enterprise_tools = []
for i in data:
# print(i['name']) # enterprise
for j in i['tools']:
# print(j)
for k in i['tools'][j]:
# print(k) # tool
enterprise_tools.append([i['name'], j, k])
return enterprise_tools
# add function
def category_tools(data):
d = []
for i in data:
for j in i['tools']:
for k in i['tools'][j]:
if [j, k] not in d:
d.append([j, k])
return d
# connect to database
var_conn = create_connection_db()
# get data from file
data = JSON_Read()
enterprises = get_enterprises(data)
tools = get_tools(data)
categories = get_categories(data)
enterprise_tool_rels = enterprise_tool(data)
category_tools_rels = category_tools(data)
# upload into database
# -------
#load_enterprise(enterprises=enterprises, cur=var_conn)
#load_categories(categories=categories, cur=var_conn)
#load_tools(tools=tools, cur=var_conn)
load_enterprise_tool(rels=enterprise_tool_rels, cur=var_conn)
#load_category_tool(rels=category_tools_rels, cur=var_conn)
# close connection database
close_connection_db(var_conn)
print('end success!!')