在日志文件中有一大堆,格式相同的文本,需要提取出接口耗時的時間
>>> 運單號:71742507538566,快遞100接口耗時:8,返回結果:[{"lengthPre":14,"comCode":"huitongkuaidi","noPre":"717425","noCount":59622}]
>>> 運單號:71679940955921,快遞100接口耗時:7,返回結果:[{"lengthPre":14,"comCode":"huitongkuaidi","noPre":"716799","noCount":65664},{"lengthPre":14,"comCode":"shentong","noPre":"716799","noCount":1}]
>>> 運單號:4601476769218,快遞100接口耗時:10,返回結果:[{"lengthPre":13,"comCode":"yunda","noPre":"460147","noCount":284021}]
>>> 運單號:4601470516612,快遞100接口耗時:9,返回結果:[{"lengthPre":13,"comCode":"yunda","noPre":"460147","noCount":284021}]
>>> 運單號:71716941576703,快遞100接口耗時:8,返回結果:[{"lengthPre":14,"comCode":"huitongkuaidi","noPre":"717169","noCount":175244}]
>>> 運單號:75308510292636,快遞100接口耗時:11,返回結果:[{"lengthPre":14,"comCode":"zhongtong","noPre":"753085","noCount":630547}]
>>> 運單號:75308440358906,快遞100接口耗時:8,返回結果:[{"lengthPre":14,"comCode":"zhongtong","noPre":"753084","noCount":812745}]
>>> 運單號:75308594459656,快遞100接口耗時:7,返回結果:[{"lengthPre":14,"comCode":"zhongtong","noPre":"753085","noCount":630547}]
>>> 運單號:777004640509373,快遞100接口耗時:10,返回結果:[{"lengthPre":15,"comCode":"shentong","noPre":"777004","noCount":497868}]
>>> 運單號:4301913501825,快遞100接口耗時:8,返回結果:[{"lengthPre":13,"comCode":"yunda","noPre":"430191","noCount":848444}]
>>> 運單號:70095194774559,快遞100接口耗時:7,返回結果:[{"lengthPre":14,"comCode":"huitongkuaidi","noPre":"700951","noCount":18888}]
>>> 運單號:777004084932865,快遞100接口耗時:7,返回結果:[{"lengthPre":15,"comCode":"shentong","noPre":"777004","noCount":497868}]
>>> 運單號:773011564374916,快遞100接口耗時:7,返回結果:[{"lengthPre":15,"comCode":"shentong","noPre":"773011","noCount":8552066},{"lengthPre":15,"comCode":"pfcexpress","noPre":"773011","noCount":8028}]
>>> 運單號:75308509589096,快遞100接口耗時:7,返回結果:[{"lengthPre":14,"comCode":"zhongtong","noPre":"753085","noCount":630547}]
>>> 運單號:4301909414706,快遞100接口耗時:7,返回結果:[{"lengthPre":13,"comCode":"yunda","noPre":"430190","noCount":1322407},{"lengthPre":13,"comCode":"tcxbthai","noPre":"430190","noCount":3}]
代碼:
import re # 該message僅作為舉例 message = '>>> 運單號:777004684640468,快遞100接口耗時:8,返回結果:[{"lengthPre":15,"comCode":"shentong","noPre":"777004","noCount":497868}]' # 正則提取出接口耗時數據 take_time = re.findall(r'快遞100接口耗時:(.*),返回結果', message) print take_time[0]
輸出:
8