你好,我有一个日志文件,文件内容如下:
[ 06-15 14:07:48.377 15012:15012 D/ViewRootImpl ]
ViewPostImeInputStage processKey 0
[ 06-15 14:07:48.397 3539: 4649 D/AudioService ]
active stream is 0x8
[ 06-15 14:07:48.407 4277: 4293 D/vol.VolumeDialogControl.VC ]
isSafeVolumeDialogShowing : false
我想从日志中提取一些信息文件。文件预期格式如下:
[('06-15 14:07:48.377', '15012', 'D', 'ViewRootImpl', 'ViewPostImeInputStage processKey 0'),
('06-15 14:07:48.397', '3539', '4649', 'D', 'AudioService', 'active stream is 0x8'),
('06-15 14:07:48.407', '4277', '4293', 'D', 'vol.VolumeDialogControl.VC', 'isSafeVolumeDialogShowing : false')]
问题:提取预期格式信息的最佳python正则表达式是什么?非常感谢!你知道吗
更新:我尝试了以下代码
import re
regex = r"(\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}.\d{3})\s(\d+).*(\w{1})/(.*)\](.*)"
data = [g.groups() for g in re.finditer(regex, log, re.M | re.I)]
我得到的结果是
data=[('06-15 14:07:48.377', '15012', 'D', 'ViewRootImpl', '\r'), (
'06-15 14:07:48.397', '3539', 'D', 'AudioService', '\r'), ('06-15 14:07:48.407',
'4277', 'D', 'vol.VolumeDialogControl.VC', '\r')]
我找不到最后一个元素。你知道吗
输出
使用以下方法:
输出:
要将结果作为匹配项列表,请使用
re.findall()
函数:输出:
相关问题 更多 >
编程相关推荐