-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt_parser.py
executable file
·195 lines (180 loc) · 6.96 KB
/
txt_parser.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Program to parse the catalog and create mongo script"""
import json
import re
def prepare_monitor(line):
mon_obj = {}
attr = line.split(',')
mon_obj['price'] = {}
mon_obj['shipping_size'] = {}
mon_obj['product_type'] = attr[0]
mon_obj['_id'] = int(attr[1])
mon_obj['price']['regular'] = float(attr[2])
mon_obj['price']['discount'] = float(attr[3])
mon_obj['shipping_size']['length'] = float(attr[4])
mon_obj['shipping_size']['breadth'] = float(attr[5])
mon_obj['shipping_size']['depth'] = float(attr[6])
mon_obj['brand'] = attr[7]
mon_obj['size'] = attr[8]
mon_obj['type'] = attr[9]
mon_obj['frequency'] = attr[10]
mon_obj['resolution'] = attr[11]
mon_obj['input_lag'] = attr[12]
#print json.dumps(mon_obj, indent=4)
return mon_obj
def prepare_television(line):
tel_obj = {}
attr = line.split(',', 14)
tel_obj['price'] = {}
tel_obj['shipping_size'] = {}
#tel_obj['resolution'] = {}
tel_obj['product_type'] = attr[0]
tel_obj['_id'] = int(attr[1])
tel_obj['price']['regular'] = float(attr[2])
tel_obj['price']['discount'] = float(attr[3])
tel_obj['shipping_size']['length'] = float(attr[4])
tel_obj['shipping_size']['breadth'] = float(attr[5])
tel_obj['shipping_size']['depth'] = float(attr[6])
tel_obj['brand'] = attr[7]
tel_obj['size'] = attr[8]
tel_obj['type'] = attr[9]
tel_obj['three_d'] = attr[10]
tel_obj['frequency'] = attr[11]
#tel_obj['resolution']['lines'] = int(re.search(r'[0-9]+', attr[12]).group(0))
#tel_obj['resolution']['type'] = re.search(r'[a-zA-Z]+', attr[12]).group(0)
tel_obj['resolution'] = attr[12]
tel_obj['input_lag'] = attr[13]
inputs = attr[14][1:-1].split(',') if (len(attr[14]) > 2) else []
tel_obj['inputs'] = inputs
#print json.dumps(tel_obj, indent=4)
return tel_obj
def prepare_desktop(line):
desk_obj = {}
attr = line.split(',', 28)
desk_obj['price'] = {}
desk_obj['shipping_size'] = {}
desk_obj['hard_drive'] = {}
desk_obj['computer_monitor'] = {}
desk_obj['hard_drive']['hdd_size'] = {}
desk_obj['product_type'] = attr[0]
desk_obj['_id'] = int(attr[1])
desk_obj['price']['regular'] = float(attr[2])
desk_obj['price']['discount'] = float(attr[3])
desk_obj['shipping_size']['length'] = float(attr[4])
desk_obj['shipping_size']['breadth'] = float(attr[5])
desk_obj['shipping_size']['depth'] = float(attr[6])
desk_obj['brand'] = attr[7]
desk_obj['num_cores'] = attr[8]
desk_obj['screen_size'] = attr[9]
desk_obj['hard_drive']['brand'] = attr[11]
desk_obj['hard_drive']['rpm'] = attr[12]
desk_obj['hard_drive']['hdd_type'] = attr[13]
desk_obj['hard_drive']['hdd_size']['size'] = int(re.search(r'[0-9]+', attr[14]).group(0))
desk_obj['hard_drive']['hdd_size']['factor'] = re.search(r'[a-zA-Z]+', attr[14]).group(0)
desk_obj['hard_drive']['form_factor'] = attr[15]
desk_obj['hard_drive']['cache_size'] = attr[16]
desk_obj['hard_drive']['interface'] = attr[17]
desk_obj['display_type'] = attr[18]
desk_obj['computer_monitor']['brand'] = attr[20]
desk_obj['computer_monitor']['size'] = attr[21]
desk_obj['computer_monitor']['type'] = attr[22]
desk_obj['computer_monitor']['frequency'] = attr[23]
desk_obj['computer_monitor']['resolution'] = attr[24]
desk_obj['computer_monitor']['input_lag'] = attr[25]
desk_obj['battery_duration'] = attr[26]
desk_obj['OS'] = attr[27]
desk_obj['peripherals'] = attr[28][1:-1].split(',') if (len(attr[28]) > 2) else []
#print json.dumps(desk_obj, indent=4)
return desk_obj
def prepare_hdd(line):
hdd_obj = {}
attr = line.split(',')
hdd_obj['price'] = {}
hdd_obj['shipping_size'] = {}
hdd_obj['product_type'] = attr[0]
hdd_obj['_id'] = int(attr[1])
hdd_obj['price']['regular'] = float(attr[2])
hdd_obj['price']['discount'] = float(attr[3])
hdd_obj['shipping_size']['length'] = float(attr[4])
hdd_obj['shipping_size']['breadth'] = float(attr[5])
hdd_obj['shipping_size']['depth'] = float(attr[6])
hdd_obj['brand'] = attr[7]
hdd_obj['rpm'] = attr[8]
hdd_obj['hdd_type'] = attr[9]
hdd_obj['hdd_size'] = attr[10]
hdd_obj['form_factor'] = attr[11]
hdd_obj['cache_size'] = attr[12]
hdd_obj['interface'] = attr[13]
#print json.dumps(hdd_obj, indent=4)
return hdd_obj
def prepare_camera(line):
cam_obj = {}
attr = line.split(',')
cam_obj['price'] = {}
cam_obj['shipping_size'] = {}
cam_obj['product_type'] = attr[0]
cam_obj['_id'] = int(attr[1])
cam_obj['price']['regular'] = float(attr[2])
cam_obj['price']['discount'] = float(attr[3])
cam_obj['shipping_size']['length'] = float(attr[4])
cam_obj['shipping_size']['breadth'] = float(attr[5])
cam_obj['shipping_size']['depth'] = float(attr[6])
cam_obj['brand'] = attr[7]
cam_obj['megapixels'] = attr[8]
cam_obj['sensor_size'] = attr[9]
cam_obj['max_iso'] = int(attr[10])
cam_obj['memory_card_type'] = attr[11]
cam_obj['camera_type'] = attr[12]
cam_obj['viewfinder_construction'] = attr[13]
cam_obj['coverage'] = float(attr[14])
cam_obj['eye_relief'] = attr[15]
cam_obj['frames_per_second'] = attr[16]
cam_obj['num_focus_points'] = int(attr[17])
cam_obj['focal_length'] = float(attr[18])
cam_obj['min_aperture'] = float(attr[19])
#print json.dumps(cam_obj, indent=4)
return cam_obj
def main():
objects = []
"""Categorizes input and calls appropriate function"""
with open('catalog.txt', 'r') as file:
for line in file:
type = line[0:3]
if (type == 'com'):
#print('Computer')
objects.append(prepare_monitor(line.strip()))
#continue
elif (type == 'tel'):
#print ('Television')
objects.append(prepare_television(line.strip()))
#continue
elif (type == 'des'):
#print ('Desktop Monitor')
objects.append(prepare_desktop(line.strip()))
#continue
elif (type == 'har'):
#print ('Hard Drive')
objects.append(prepare_hdd(line.strip()))
#continue
elif (type == 'dig'):
#print ('Digital Camera')
objects.append(prepare_camera(line.strip()))
#continue
else:
continue
#print json.dumps(objects, indent=4)
file = open('catalogInsertion.js', 'w')
file.write('db.product.insert(' + json.dumps(objects, indent=4) + ')')
file.close
#def create_file():
# with open('catalog.txt', 'r') as infile:
# with open('camera.txt', 'w') as outfile:
# lines = []
# for line in infile:
# if (line[0:3] == 'dig'):
# lines.append(line)
# outfile.writelines(lines)
# print ('successful!!!!!!!!!!!!!')
if __name__ == "__main__":
main()