-
Notifications
You must be signed in to change notification settings - Fork 0
/
glossary.py
88 lines (56 loc) · 1.64 KB
/
glossary.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
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 29 14:19:00 2017
@author: brnissen & Swathi
"""
# Imports for command line and regex
#import sys
import re
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser # ver. < 3.0
# instantiate
config = ConfigParser()
# parse existing file
config.read('test.ini')
# read values from a section
in_file = config.get('section_a', 'in_file')
out_file = config.get('section_a', 'out_file')
regex = config.get('section_a', 'regex')
part = config.get('section_a', 'part')
#print(in_file,out_file, regex, part)
#in_file = sys.argv[1]
#out_file = sys.argv[2]
input_f = open(in_file, 'r')
#print (input_f.read())
#file = input_f.read()
lines = input_f.readlines()
#print (lines)
temp_lines = [] # Lines that have glossary entries
#regex = "^><B>.*"
for line in lines:
#print(line)
if (re.search(regex, line)):
temp_lines.append(line + '\n')
#print(temp_lines)
#print (temp_lines[1])
d = {}
#part = '</B>'
for line in temp_lines:
line_part = line.partition(part)
if re.search('\(</B>',line): # Look for parentheses in key that should be in value
print("( exists")
key = line_part[0][4:-1]
value = '(' + line_part[2]
else:
key = line_part[0][4:]
value = line_part[2]#[:-3]
d[key] = value
#print(d)
out_f= open(out_file, 'w')
for key, value in d.items():
out_f.write("{}\t{}\n".format(key, value))
#out_f.write(str(d))
out_f.close()
input_f.close()