Match multiline regex in file object

核能气质少年 提交于 2019-11-30 12:11:45
Mike

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!