I wrote a script to find a certain string and print a certain occurrence. This worked very well until the groups become an indeterminate length and I simply need to print the last one. I was curious for the found2.group and found3.group if there was a way to simply only print the last result.
f = open ("CompTime.csv","w")
for infile in glob.glob( os.path.join(dir, '*.out') ):
file_handler = open(infile, "r")
content = file_handler.read()
file_handler.close()
#Find Real Time
found2 = re.search(' REAL TIME *.+', content)
rtime = found2.group(0)[1:-1]
#Find CPU Time
found3 = re.search(' CPU TIMES *.+', content)
ctime = found3.group(0)[1:-1]
#Create and Format the results.
tResult = str(rtime)+','+str(ctime)
f.seek(0,2)
f.write(tResult+'\n')
f.close()
Is there any way to do this simply I have read the literature on the regular expressions but I seem to be failing at completing this.
Working Version:
dir = os.getcwd()
for infile in glob.glob( os.path.join(dir, '*.out') ):
file_handler = open(infile, "r")
content = file_handler.read()
file_handler.close()
rtime = re.findall(' REAL TIME *.+', content)[-1]
#Find CPU Time
ctime = re.findall(' CPU TIMES *.+', content)[-1]
#Create and Format the results.
tResult = str(rtime)+','+str(ctime)
print tResult
I believe that re.match only looks for first occurence, you can use re.findall(pattern, string) instead:
Then you can just access it like any other list: