python中查詢文件中包含某字符串的行

import codecs

recordFile = 'execute-records'

FoundFlag = False
fileObj = codecs.open(recordFile, 'r+', 'utf-8')
lineTemp = fileObj.readlines()
count = 0
server_no="merc"
for line in lineTemp:
    # 等於-1表示匹配不到,非-1表示匹配到的索引
    if line.strip().find(server_no) != -1:
        FoundFlag = True
        count +=1
        print("the line is: " + line, end='')

if FoundFlag == False:
    print("Not found the string!")
print("匹配個數:%d" % count)
fileObj.close()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章