python|python 读取ng日志并根据关键字分离出相关行数据

运维时需要分析ng的log日志, 然后根据关键字读取相关行数据出来分析, 避免打开整个文件太大导致缓慢.


代码如下
'''
读取keyWordName关键字的出现次数
'''
# 计数
count =0
# 每次读取文件内容大小
sizeInt =1000000
matchList = []
# 查询关键字
def findWordCount(f, sizeInt, keyWordName):
global count
count =0;
while True:
line = f.readline(sizeInt)
if not line:
break
pass
lineStr =str(line)
if lineStr.find(keyWordName) > -1:
matchList.append(lineStr)
count +=1
# print(lineStr)
return count
【python|python 读取ng日志并根据关键字分离出相关行数据】# 打印
def printKeywordCount(f, sizeInt, keyWordName):
count = findWordCount(f, sizeInt, keyWordName)
print(count)
print(keyWordName +"出现次数: " +str(count))
# 查询文件里关键字出现次数
def findTheKeyWordCount(fileName, keyWordName):
with open(fileName, "r", encoding="utf-8", errors='ignore')as f:
printKeywordCount(f, sizeInt, keyWordName)
def saveMatchList(saveFileName, matchList):
contents =""
for iin range(0, len(matchList)):
contents += matchList[i] +"\n"
save2File(saveFileName, contents)
# 保存文件
def save2File(fileName, content):
f =open(fileName, "w")
num = f.write(content)
print(num)
# 关闭打开的文件
f.close()
# 执行工作
def doMyJob(fileName, keyWordName, saveFileName):
# 查询关键字
findTheKeyWordCount(fileName, keyWordName)
# 打印
# print(matchList)
# 保存文件
saveMatchList(saveFileName, matchList)
if __name__ =='__main__':
# 文件所在目录
fileName ="D:/code/workspace/python/test1/test/files/error.log"
# 需要查询的关键字
keyWordName ="[error]"
# 解析后的内容保存文件
saveFileName ="D:/code/workspace/python/test1/test/files/error_logs_20180704.txt"
doMyJob(fileName, keyWordName, saveFileName)

    推荐阅读