I have a text file which contains several lines in the following format:
ELEMENT= 1 PLY= 1
-----------------------
Code 1425
GP= 1 4.324E-03 -1.350E-03 -2.974E-03 3.084E-04 0.000E+00 0.000E+00
GP= 2 1.435E-03 -3.529E-04 -1.082E-03 1.183E-04 0.000E+00 0.000E+00
GP= 3 7.742E-03 -3.542E-03 -4.200E-03 4.714E-04 0.000E+00 0.000E+00
GP= 4 4.842E-03 -2.378E-03 -2.463E-03 3.040E-04 0.000E+00 0.000E+00
The number after the word ELEMENT
goes from 1 to 60. My first goal is to read this text file and stop to every occurrence of the word ELEMENT = 1
to ELEMENT = 60
My test script reads the first occurrence of ELEMENT
. I would now like to go through the 60 occurrences of ELEMENT
, so I have tried to implement a variable following ELEMENT
, in this example I have initialized it to 2 to see if it would work and as you can guess it doesn't (see example code below).
elem= 2
lines = open("myfile.txt", "r" ).readlines()
for line in lines:
if re.search( r"ELEMENT= %i" (line, elem) ):
words = line.split()
energy = float( words[1] )
print "%f" % energy
break
I get the following error code:
File "recup.py", line 42, in <module>
if re.search( r"ELEMENT= %i" (line, elem) ):
TypeError: 'str' object is not callable
My question then is how would I implement a variable into my search?