-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_food.py
More file actions
38 lines (33 loc) · 784 Bytes
/
database_food.py
File metadata and controls
38 lines (33 loc) · 784 Bytes
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
# use the data downloaded from http://www.ars.usda.gov/Services/docs.htm?docid=23634
# save the data with encoding utf-8
import sqlite3
def convert(value):
if value.startswith('~'):
return value.strip('~')
if not value:
value = '0'
return float(value)
conn = sqlite3.connect('./Sample_Database/food.db')
curs = conn.cursor()
curs.execute('''
CREATE TABLE food(
id TEXT PRIMARY KEY,
desc TEXT,
water FLOAT,
kcal FLOAT,
protein FLOAT,
fat FLOAT,
ash FLOAT,
carbs FLOAT,
fiber FLOAT,
sugar FLOAT
)
''')
query = 'INSERT INTO food VALUES (?,?,?,?,?,?,?,?,?,?)'
with open('./Sample_Database/sr26abbr/ABBREV.txt') as f:
for line in f:
fields = line.split('^')
vals = [convert(F) for F in fields[:10]]
curs.execute(query,vals)
conn.commit()
conn.close()