-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
84 lines (58 loc) · 1.81 KB
/
parse.py
File metadata and controls
84 lines (58 loc) · 1.81 KB
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
import os, sys, re, copy
fn = 'IT-GSK-13-EL-en-all_v940-2.md'
f = open(fn,"rt")
f.read(1)
headingRegex = "^# T [0-9]{1,3}"
headingRegexComp = re.compile(headingRegex)
exampleRegex = "^## Example(|s):"
exampleRegexComp = re.compile(exampleRegex)
# ueberschrift -> (example) ueberschrift
# example -> uebershrift
counter = 0
stateHeading = 1
stateExample = 2
state = -1
resultlist = []
result = None
template = {"heading" : "", "description" : "", "examples" : ""}
for line in f:
counter += 1
if headingRegexComp.match(line):
if result:
resultlist.append(result)
print(result)
result = copy.deepcopy(template)
print("Head!")
result["heading"] = line.strip()
state = stateHeading
elif state == stateHeading and not exampleRegexComp.match(line):
print("Body!")
result["description"]+=line ### 1
elif state == stateHeading and exampleRegexComp.match(line):
state = stateExample ### 2
print("Start Example!")
elif state == stateExample:
if not headingRegexComp.match(line):
print("Example!")
result["examples"]+=line
else:
print("Error in line %i!" % counter)
exit()
else:
print("Fatal Error") ### 3 Was not reachable
exit()
#pip install xlsxwriter
# import xlsxwriter
# workbook = xlsxwriter.Workbook('out.xlsx')
# worksheet = workbook.add_worksheet()
# row = 0
# col = 0
# for r in resultlist:
# worksheet.write(row,col,r["heading"])
# worksheet.write(row,col+1,r["description"])
# worksheet.write(row,col+2,r["examples"])
# row+=1
# workbook.close()
import json
with open('out.json', 'w') as outfile:
json.dump(resultlist, outfile)