How can I extract the groups from this regex from a file object (data.txt)?
import numpy as np
import re
import os
ifile = open(\"data.txt\",\'r\')
# Regex
You can read the data from the file object into a string with ifile.read()
Why don't you read the whole file into a buffer using
buffer = open("data.txt").read()
and then do a search with that?
times = [match.group(1) for match in pattern.finditer(ifile.read())]
finditer yield MatchObjects. If the regex doesn't match anything times
will be an empty list.
You can also modify your regex to use non-capturing groups for storeU
, storeI
, iIx
and avgCI
, then pattern.findall
will contain only matched times.
Note: naming variable time
might shadow standard library module. times
would be a better option.