Skip to content
Advertisement

Extracting various variables from a file in Python

I have a file that looks like

Output.txt 

A= 10 
B= -2.0 
C= 0.3

E. E.= 0.0077085100262409825
#Other stuff
E. E.= 0.007579616077337539
#Other stuff
E. E.= 0.007516578218920226
#Other stuff
E. E.= 0.007516578218913118
#Other stuff
E. E.= 0.007516578218910091
#Other stuff
E. E.= 0.007516578218925583
#Other stuff
E. E.= 0.00751656967834972
#Other stuff

a_{0} = 0.99638864684082906198
a_{1} = 0.99545718205037281301
a_{2} = 0.99810837983673184048
a_{3} = 0.99811186196548795646
a_{4} = 0.99811633864785687109
a_{5} = 0.99811633864785698211
a_{6} = 0.99811186196548884464
a_{0} a_{1} = 0.99544942851733930755
a_{1} a_{2} = 0.99451063074121059948
a_{2} a_{3} = 0.99716648871966484524
a_{3} a_{4} = 0.99716998943318368998
a_{4} a_{5} = 0.99717446741570991975
a_{5} a_{6} = 0.99716998943318446713

I would like to extract the values of “A”, “B”, “C”, the last value of “E. E.” and all values of “a_{i}” and “a_{i}a_{j}”. To get A, B, and C, I have used this code as I know exactly the lines’ numbers

ftxt="Output.txt"
with open(ftxt) as fp:
    for i, line in enumerate(fp):
        if i == 0:
            print(line)
            match = re.search(r'd+.?d*', line)
            if match:
                A=float(match.group())
        elif i == 1:
            print(line)
            match = re.search(r'd+.?d*', line)
            if match:
                B=float(match.group())
        elif i == 2:
            print(line)
            match = re.search(r'd+.?d*', line)
            if match:
                C=float(match.group())
        else:
            break

How can I extract the rest? For the last “E.E.” the output should be “0.00751656967834972”. For “a_{i}” and “a_{i}a_{j}”, I would like to have an array with indices i and j as well as the values of each variable.

Advertisement

Answer

How about using dictionaries instead of creating variables on the fly?

import re

p1 = re.compile(r'([A-Z]|(?:E. E.))s*=s*(-?d+(?:.d+)?)s*$')
p2 = re.compile(r'a_{(d+)}s=s*(d+.d+)s*$')
p3 = re.compile(r'a_{(d+)}s*a_{(d+)}s=s*(d+.d+)s*$')

data = '''
A= 10 
B= -2.0 
C= 0.3

E. E.= 0.0077085100262409825
#Other stuff
E. E.= 0.007579616077337539
#Other stuff
E. E.= 0.007516578218920226
#Other stuff
E. E.= 0.007516578218913118
#Other stuff
E. E.= 0.007516578218910091
#Other stuff
E. E.= 0.007516578218925583
#Other stuff
E. E.= 0.00751656967834972
#Other stuff

a_{0} = 0.99638864684082906198
a_{1} = 0.99545718205037281301
a_{2} = 0.99810837983673184048
a_{3} = 0.99811186196548795646
a_{4} = 0.99811633864785687109
a_{5} = 0.99811633864785698211
a_{6} = 0.99811186196548884464
a_{0} a_{1} = 0.99544942851733930755
a_{1} a_{2} = 0.99451063074121059948
a_{2} a_{3} = 0.99716648871966484524
a_{3} a_{4} = 0.99716998943318368998
a_{4} a_{5} = 0.99717446741570991975
a_{5} a_{6} = 0.99716998943318446713
'''

d = {}
a = {}
aa = {}
for line in data.splitlines(True):  # or for line in open(path)
    m = p1.match(line)
    if m:
        k, v = m.groups()
        d[k] = float(v)
        continue

    m = p2.match(line)
    if m:
        i, v = m.groups()
        a[int(i)] = float(v)
        continue

    m = p3.match(line)
    if m:
        i, j, v = m.groups()
        aa[(int(i), int(j))] = float(v)
        continue

print(d)
print(a)
print(aa)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement