Say I had an input file (temp.tmpl) that looked like this:
PTF @
ARB @ C @ @ A @ @ C @
OSN @ B @ @ A @
SDA @ B @
CPN 3.23
SNL 3.26
And in some other file (candidate.txt):
A 3.323 B 4.325 C 6.32 D 723 E 8 F 9 G 1.782
H 7
I 4
J 9
K 10
And I wanted to replace A, B , and C with their assigned values. The way this needs to be accomplished for my task is by finding the variables A, B, and C by looking for @ @...Then knowing this is obviously a varible. Then replacing them. This is what I have tried:
reader = open('candidate.txt', 'r')
out = open('output.txt', 'w')
dictionary = dict()
for line in reader.readlines():
pairs = line.split()
for variable, value in zip(pairs[::2],pairs[1::2]):
dictionary[variable] = value
#Now to open the template file
template = open('temp.tmpl', 'r')
for line1 in template:
if line1[1]:
confirm = line1.split(' ')[0].lower()
symbol = line1.split(' ')[1]
if confirm == 'ptf':
next(template)
elif symbol in line1:
start = line1.find(symbol)+len(symbol)
end = line1[start:].find(symbol)
variable = line1[start:start + end].strip()
print variable
And I can not seem to figure out how to handle the lines with multiple sets of variables.
Thank you so much in advance.
re.sub?