-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2sqlite.py
More file actions
94 lines (87 loc) · 2.79 KB
/
json2sqlite.py
File metadata and controls
94 lines (87 loc) · 2.79 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
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
import sqlite3
import json
import os
import glob
# データベース作成
conn = sqlite3.connect('fish.db')
cursor = conn.cursor()
# reportテーブル作成
def create_tables():
cursor.execute('''
CREATE TABLE IF NOT EXISTS report (
id INTEGER PRIMARY KEY AUTOINCREMENT,
facility TEXT,
date TEXT,
weather TEXT,
water_temp REAL,
tide TEXT,
visitors INTEGER,
sentence TEXT
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS catch (
id INTEGER PRIMARY KEY AUTOINCREMENT,
report_id INTEGER,
fish_name TEXT,
min_size REAL,
max_size REAL,
unit TEXT,
count INTEGER,
place TEXT,
FOREIGN KEY(report_id) REFERENCES report(id)
)
''')
conn.commit()
def insert_report(item):
cursor.execute(
'INSERT INTO report (facility, date, weather, water_temp, tide, visitors, sentence) VALUES (?, ?, ?, ?, ?, ?, ?)',
(
item.get('facility'),
item.get('date'),
item.get('weather'),
float(item.get('waterTemp')) if item.get('waterTemp') else None,
item.get('tide'),
item.get('visitors'),
item.get('sentence')
)
)
return cursor.lastrowid
def insert_catches(item, report_id):
for i in range(1, 31):
name = item.get(f'fish{i}Name')
if not name:
continue
min_size = item.get(f'fish{i}MinSize')
max_size = item.get(f'fish{i}MaxSize')
unit = item.get(f'fish{i}Unit')
count = item.get(f'fish{i}Count')
place = item.get(f'fish{i}Place')
place_str = ','.join(place) if place else None
cursor.execute(
'INSERT INTO catch (report_id, fish_name, min_size, max_size, unit, count, place) VALUES (?, ?, ?, ?, ?, ?, ?)',
(report_id, name, min_size, max_size, unit, count, place_str)
)
def process_json_file(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
data = json.load(f)
# もしdataがstr型なら再度デコード
if isinstance(data, str):
data = json.loads(data)
items = data['data']['lastPostsByFacilityAndDate']['items']
for item in items:
report_id = insert_report(item)
insert_catches(item, report_id)
conn.commit()
def main():
create_tables()
base = 'location'
for facility in os.listdir(base):
dir_path = os.path.join(base, facility)
if not os.path.isdir(dir_path):
continue
for json_file in glob.glob(os.path.join(dir_path, '*.json')):
process_json_file(json_file)
print('Done!')
if __name__ == '__main__':
main()