- 论坛徽章:
- 11
|
本帖最后由 bskay 于 2018-03-14 13:24 编辑
#首先,分析数据格式:
s='''
Active connections: 760
server accepts handled requests request_time
5006365693 5006365693 6791022492 116870885316
Reading: 0 Writing: 14 Waiting: 746
'''
# 这样,人工自然的按行,按字段划分
text =[
['Active', 'connections:', '760'],
['server', 'accepts', 'handled', 'requests', 'request_time'],
['5006365693', '5006365693', '6791022492', '116870885316'],
['Reading:', '0', 'Writing:', '14', 'Waiting:', '746 '],
]
# 然后把上面需要的数据字段单个替换
text =[
['Active', 'connections:', '(?P<connections>\d+)'],
['server', 'accepts', 'handled', 'requests', 'request_time'],
['(?P<tengine_requests>\d+)', '(?P<tengine_accepts>\d+)', '(?P<tengine_handled>\d+)', '(?P<request_time>\d+)'],
['Reading:', '\d+', 'Writing:', '\d+', 'Waiting:', '\d+'],
]
#然后用 '\s*'连接行,'\s+'连接字段,进行 匹配
re.match('\s*'.join(['\s+'.join(l) for l in text]), s).groupdict()
#如果有多个,用
re.findall('\s*'.join(['\s+'.join(l) for l in text]), s)
|
|