python 日志內容提取


 

問題:如下,一個很大的日志文件,提取 start: 到 end: 標志中間的內容

日志文件a.log

xxxxx
yyyyy
start:
start:
hahahaha
end:
start:
hahahahha
end:
ccccccc
kkkkkkk
cdcdcdcd
start:
hahahaha
end:

 

code

import re

isfind=False
with open("a.log","r") as f:
    while(1):
        l=f.readline()
        if(not l):
            break
        if(re.match("start:", l)):
            isfind=True
        if(isfind):
            print(l)
            if(re.match("end:", l)):
                isfind=False

輸出

start:
start:
hahahaha
end:
start:
hahahahha
end:
start:
hahahaha
end:

 

參考:

https://www.cnblogs.com/aaronthon/p/9435967.html

 


免責聲明!

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



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