-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_data.py
53 lines (42 loc) · 1.29 KB
/
update_data.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
import sys
import os
import json
if len(sys.argv) < 2:
print('no origin data')
quit()
origin_dir = sys.argv[1]
def parse_poem(poem_path):
with open(poem_path, 'r')as file:
lines = list(map(lambda s: s.strip(), file.readlines()))
title = lines[0][6:]
date = lines[1][5:]
lines = lines[3:]
return {'t': title, 'd': date, 'l': lines}
def parse_poet(dir_name, root):
try:
split = dir_name.rindex('_')
poet = {'n': dir_name[:split], 'p': dir_name[split+1:]}
works = []
for root, _, fs in os.walk(os.path.join(root, dir_name)):
for f in fs:
if f.endswith('.pt'):
poem = parse_poem(os.path.join(root, f))
works.append(poem)
poet['w'] = works
return poet
except IOError:
return None
if not os.path.exists('data'):
os.makedirs('data')
# Delete previous data
for f in os.listdir('data'):
file_path = os.path.join('data', f)
if os.path.isfile(file_path):
os.remove(file_path)
poets = []
for root, ds, _ in os.walk(origin_dir):
for d in ds:
poet = parse_poet(d, root)
if poet:
with open(os.path.join('data', poet['n']), 'w') as file:
file.writelines(json.dumps(poet))