斯蒂芬·勞赫給出的答案是很酷,但我認爲你是python的新手,所以這裏是一個基本功能。
考慮,「START」必須始終在消息的開始,而不是之間像,
[739:247021] PHYThrad: START DSPMsgQ Received: msg type is[130] and SFNSF [14997] [SFN:937 SF:5] # START at second index after split.
如果我們考慮以上使用的情況下,這裏是可以打印本"START"
線功能在日誌消息開始的文件中。
def getStart(filename):
with open(filename, "r") as reader:
for lines in reader.readlines(): # get list of lines
start = lines.split(' ')[1] # Split with space, and check the word is "START"
if start =='START':
print lines
getStart("a.txt") # considering your filename is a.txt.
輸出:
[739:246050] START of MACThread:receved msg type[47]
[739:247059] START of MACThread:receved msg type[47]
爲什麼你使用'__contains__',而不是'in'? –