2012-04-19 57 views
0

我想從一個txt文件中提取小時,分鐘,秒和mseconds,這可能會或可能不會出現在一行中。格式是「hh:mm:ss.ms」。我知道我應該這樣這樣的事情用Python搜索多個項目

int(re.search('(\d+):(\d+):(\d+).(\d+)', current_line).group(1)) 

,但我不知道如何將這些四個值返回到四個不同的變量。

回答

2

好吧,如果你堅持在一線做:

hrs, min, sec, msec = (int(group) for group in re.search('(\d+):(\d+):(\d+).(\d+)', current_line).groups()) 
3

可以調用匹配對象groups獲取組的元組,像這樣:

match = re.search('(\d+):(\d+):(\d+).(\d+)', current_line) 
hour,minute,second,ms = map(int, match.groups())