-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.py
49 lines (38 loc) · 1.17 KB
/
load.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
import csv
import json
import sys
from utils_api import get_database
clientDB = get_database()
def load_csv_to_database():
CSV_PATH = '../data/animal_detail.csv'
animals = []
with open(CSV_PATH) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
animals.append(row)
animalCollection = clientDB["animals"]
animalCollection.insert_many(animals)
def insert_document(path_to_document):
with open(path_to_document) as jsonfile:
zones = json.load(jsonfile)
locationsCollection = clientDB["locations"]
locationsCollection.insert_many(zones)
def update_document(path_to_document):
with open(path_to_document) as jsonfile:
updates = json.load(jsonfile)
animalsCollection = clientDB["animals"]
for update in updates:
animalsCollection.update_one(update['data'], update['updated'])
if __name__ == "__main__":
if sys.argv[1] == "insert":
if len(sys.argv) < 3:
print("provide path")
else:
insert_document(sys.argv[2])
elif sys.argv[1] == "update":
if len(sys.argv) < 3:
print("provide path")
else:
update_document(sys.argv[2])
# print(sys.argv)
# insert_document("data/locations.json")