python:字符串中提取特定的數據


在日志文件中有一大堆,格式相同的文本,需要提取出接口耗時的時間

>>> 運單號: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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM