-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
36 lines (31 loc) · 1.13 KB
/
convert.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
# -*- coding: utf-8 -*-
import json
fr = 'source/produktiLG.txt'
to = 'static/js/data/products.lg.js'
# {'id': 1, 'name': 'Augļi un ogas', 'products': [
# {'id': 1, 'name': 'Āboli', 'calories': 49, 'protein': 0, 'hydrates': 12, 'fat': 0, 'cholesterol': 0},
# {'id': 2, 'name': 'Ananāss', 'calories': 49, 'protein': 0, 'hydrates': 12, 'fat': 0, 'cholesterol': 0},
# {'id': 3, 'name': 'Apelsīni', 'calories': 41, 'protein': 1, 'hydrates': 9, 'fat': 0, 'cholesterol': 0}
# ]},
category = []
c_id = 1
pr_id = 1
def c(s):
s = s.strip()
if '.' in s:
return float(s)
if s == u'':
return 0
return int(s)
for l in open(fr).read().split("\n"):
# print l
data = l.strip().split("\t")
# print data
if len(data) == 1 and data[0] != '':
# start new category
category.append({'id': c_id, 'name': data[0], 'products': []})
c_id += 1
if len(data) > 1:
category[-1]['products'].append({'id': pr_id, 'name': data[0], 'calories': c(data[1]), 'protein': c(data[3]), 'hydrates': c(data[4]), 'fat': c(data[5]), 'cholesterol': c(data[6])})
pr_id += 1
open(to, 'w').write(u'var __products = {json};'.format(json = json.dumps(category)))