Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to get variable from between multiple occurrences in string

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.

like image 724
user1497892 Avatar asked May 20 '26 17:05

user1497892


1 Answers

Using an re? The question was altered, and here is my modified solution:

import re

# Create translation dictionary
codes = re.split(r'\s',open('candidate.txt').read())
trans = dict(zip(codes[::2], codes[1::2]))

outfh = open('out.txt','w')
infh  = open('data.txt')

# First line contains the symbol, but has a trailing space!
symbol = re.sub(r'PTF (.).*',r'\1', infh.readline()[:-1])

for line in infh:
    line = re.sub('\\'+ symbol + r' ([ABC]) ' + '\\' + symbol,
               lambda m: '%s %s %s' % (symbol,trans[m.groups()[0]],symbol),
               line)
    outfh.write(line) 

outfh.close()

The dict using two zips is a trick to create a dictionary from a [key,value,key,value,...] list

trans is a dictionary with the names and their respective values.
r'@ ([ABC]) @' captures either A or B or C within the @ signs The lambda function is passed a match object, on which we are calling the groups() method. This returns a tuple of the matching parentheses groups, in this case either A or B or C. We use this for the key to the dictionary trans, and hence replace it with the value.

like image 130
cdarke Avatar answered May 22 '26 23:05

cdarke