I have a file that looks like
JavaScript
x
35
35
1
Output.txt
2
3
A= 10
4
B= -2.0
5
C= 0.3
6
7
E. E.= 0.0077085100262409825
8
#Other stuff
9
E. E.= 0.007579616077337539
10
#Other stuff
11
E. E.= 0.007516578218920226
12
#Other stuff
13
E. E.= 0.007516578218913118
14
#Other stuff
15
E. E.= 0.007516578218910091
16
#Other stuff
17
E. E.= 0.007516578218925583
18
#Other stuff
19
E. E.= 0.00751656967834972
20
#Other stuff
21
22
a_{0} = 0.99638864684082906198
23
a_{1} = 0.99545718205037281301
24
a_{2} = 0.99810837983673184048
25
a_{3} = 0.99811186196548795646
26
a_{4} = 0.99811633864785687109
27
a_{5} = 0.99811633864785698211
28
a_{6} = 0.99811186196548884464
29
a_{0} a_{1} = 0.99544942851733930755
30
a_{1} a_{2} = 0.99451063074121059948
31
a_{2} a_{3} = 0.99716648871966484524
32
a_{3} a_{4} = 0.99716998943318368998
33
a_{4} a_{5} = 0.99717446741570991975
34
a_{5} a_{6} = 0.99716998943318446713
35
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
JavaScript
1
21
21
1
ftxt="Output.txt"
2
with open(ftxt) as fp:
3
for i, line in enumerate(fp):
4
if i == 0:
5
print(line)
6
match = re.search(r'd+.?d*', line)
7
if match:
8
A=float(match.group())
9
elif i == 1:
10
print(line)
11
match = re.search(r'd+.?d*', line)
12
if match:
13
B=float(match.group())
14
elif i == 2:
15
print(line)
16
match = re.search(r'd+.?d*', line)
17
if match:
18
C=float(match.group())
19
else:
20
break
21
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?
JavaScript
1
67
67
1
import re
2
3
p1 = re.compile(r'([A-Z]|(?:E. E.))s*=s*(-?d+(?:.d+)?)s*$')
4
p2 = re.compile(r'a_{(d+)}s=s*(d+.d+)s*$')
5
p3 = re.compile(r'a_{(d+)}s*a_{(d+)}s=s*(d+.d+)s*$')
6
7
data = '''
8
A= 10
9
B= -2.0
10
C= 0.3
11
12
E. E.= 0.0077085100262409825
13
#Other stuff
14
E. E.= 0.007579616077337539
15
#Other stuff
16
E. E.= 0.007516578218920226
17
#Other stuff
18
E. E.= 0.007516578218913118
19
#Other stuff
20
E. E.= 0.007516578218910091
21
#Other stuff
22
E. E.= 0.007516578218925583
23
#Other stuff
24
E. E.= 0.00751656967834972
25
#Other stuff
26
27
a_{0} = 0.99638864684082906198
28
a_{1} = 0.99545718205037281301
29
a_{2} = 0.99810837983673184048
30
a_{3} = 0.99811186196548795646
31
a_{4} = 0.99811633864785687109
32
a_{5} = 0.99811633864785698211
33
a_{6} = 0.99811186196548884464
34
a_{0} a_{1} = 0.99544942851733930755
35
a_{1} a_{2} = 0.99451063074121059948
36
a_{2} a_{3} = 0.99716648871966484524
37
a_{3} a_{4} = 0.99716998943318368998
38
a_{4} a_{5} = 0.99717446741570991975
39
a_{5} a_{6} = 0.99716998943318446713
40
'''
41
42
d = {}
43
a = {}
44
aa = {}
45
for line in data.splitlines(True): # or for line in open(path)
46
m = p1.match(line)
47
if m:
48
k, v = m.groups()
49
d[k] = float(v)
50
continue
51
52
m = p2.match(line)
53
if m:
54
i, v = m.groups()
55
a[int(i)] = float(v)
56
continue
57
58
m = p3.match(line)
59
if m:
60
i, j, v = m.groups()
61
aa[(int(i), int(j))] = float(v)
62
continue
63
64
print(d)
65
print(a)
66
print(aa)
67