-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
58 lines (45 loc) · 1.52 KB
/
database.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
from database_access import DatabaseCreator
def parse_tsv_file(file):
lines = []
try:
with open(file, 'r', encoding='utf-8') as reader:
# skip column info line
next(reader)
for line in reader:
line = line.split("\t")
lines.append(line)
except Exception as e:
print(e)
return lines
def parse_json_file(file):
import json
jsons = []
try:
with open(file, 'r', encoding='utf-8') as reader:
for line in reader:
line = json.loads(line)
jsons.append(line)
except Exception as e:
print(e)
return jsons
db_file = r"amazon_reviews_us_Watches_v1_00.db"
tsv_file = r"./data/amazon_reviews_us_Watches_v1_00.tsv"
json_review_file = "sample_review_json.json"
json_product_file = "sample_product_json.json"
# create tables
db_creator = DatabaseCreator(db_file)
db_creator.create_table("Product", db_creator.product_columns)
db_creator.create_table("Review", db_creator.review_columns)
# get data
reviews = parse_tsv_file(tsv_file)
with db_creator.conn:
# insert data
for review in reviews:
product_info = (review[3], review[5], review[6])
review_info = (review[0], review[1], review[3],
review[7], review[8], review[11], review[12],
review[13], review[14].split("\n")[0])
# insert product
db_creator.insert_product(product_info)
# insert review
db_creator.insert_review(review_info)