forked from lavender010101/do_my_marx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.py
36 lines (36 loc) · 1.23 KB
/
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
import json
result = []
with open("tq/毛泽东思想和中国特色社会主义理论体系概论题库.txt", "r") as f:
txt = f.read()
problems = txt.split("\n\n")
for problem in problems:
lines = problem.split("\n")
p = {}
for line in lines:
if line == "":
pass
elif line.startswith("A"):
p['A'] = line
elif line.startswith("B"):
p['B'] = line
elif line.startswith("C"):
p['C'] = line
elif line.startswith("D"):
p['D'] = line
elif line.startswith("正确答案"):
p['answer'] = line.replace("正确答案: ", "")
else:
p['problem'] = line
try:
if p['answer'] == "对" or p['answer'] == "错":
p['type'] = "判断题"
elif len(p['answer']) > 1:
p['type'] = "多选题"
else:
p['type'] = "单选题"
result.append(p)
except KeyError:
print(p)
# print(result)
with open("毛泽东思想和中国特色社会主义理论体系概论题库.json", "w") as f:
f.write(json.dumps(result, ensure_ascii=False))