-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonline2md.py
36 lines (29 loc) · 930 Bytes
/
jsonline2md.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
import glob
import json
def process():
img_with_link = r'[![alt text](res\%s)](%s)'
path_in = glob.glob(r'res\bti_*.json')
if len(path_in) > 1:
print('more than one Scrapy result, use latest one.')
elif len(path_in) == 0:
raise ValueError('No Scrapy result find.')
path_in = sorted(path_in)[-1]
f_out = open(r'res.md', 'w', encoding='utf-8')
f_in = open(path_in, encoding='utf-8')
f_out.write('''name | type | price | family_price | image
--- | --- | --- | --- | ---
''')
for line in f_in:
d = json.loads(line, encoding='utf-8')
f_out.write(' | '.join([
d['name'],
d['type'],
d['package_price'],
d['family_price'],
img_with_link % (d['image_urls'][0].split('/')[-1], d['url'])
]))
f_out.write('\n')
f_in.close()
f_out.close()
if __name__ == "__main__":
process()