日志分析,计算每个接口的调用时间+读写文件

  • Post author:
  • Post category:其他


包含知识点:

1.读取和写入文件

2.字符串与datatime时间互转,datetime时间间隔计算

3.判断字符串是否包含,字符串查找,去空格

4.字符串分割,获取字符串中的某些元素

5.全局声明,返回多个参数

#coding:gbk
import numpy
import string
import datetime
import time

flag1 = 'start'
flag2 = 'finish'
timelength = 24
strTime1 = ''
strTime2 = ''
strName1 = ''
strName2 = ''
lastTime = ''
lastName = ''
timeFormat = '%Y-%m-%d %H:%M:%S:%f'#时间格式
allSaveData = ''

def SaveInfo(line,flag,strTime,strName):
    global allSaveData#声明为全局变量
    if(flag in line):#判断字符串line是否包含字符串flag
        startIndex = line.find(flag)#查找flag在line中位置
        interfaceStr = line[startIndex:]#获取从某一位置到结束的字符串
        strTime = line[1:timelength]#获取从1到x处的字符串
        allSaveData += strTime + '--' + interfaceStr#字符串拼接
        ifArr = interfaceStr.split('|')#字符串分割
        if(len(ifArr) < 3):#判断数组的长度
            return 0,'-1','-1'
        lastIFName = ifArr[-2]#获取数组中倒数第二个元素
        strName = lastIFName.strip()#去除空格
        return 1,strTime,strName#多个返回参数
    return 0,'-2','-2'

def LoadData(strfile):
    global allSaveData,strName1,strName2,strTime1,strTime2
    lines=[]
    with open(strfile) as pFile:#打开文件
        """
        读取所有文件,注意读取后文件指针偏移至最后
        allData = (pFile.read())
        """
        lines = pFile.readlines()#读取所有行
    #逐行读取文件
    for line in lines:
        res,strTime,strName = SaveInfo(line,flag1,strTime1,strName1)
        if(res == 1):
            strTime1 = strTime
            strName1 = strName
        res,strTime,strName = SaveInfo(line,flag2,strTime2,strName2)
        if(res == 1):
            strTime2 = strTime
            strName2 = strName
        if((strName1 == strName2) and strName1!=''):
            #字符串转datetime
            time1  = datetime.datetime.strptime(strTime1,timeFormat)
            time2  = datetime.datetime.strptime(strTime2,timeFormat)
            #datetime时间间隔计算
            timeInterval = (time2-time1).total_seconds()
            if timeInterval>0.1:
                print (strName1,'--',timeInterval)
            allSaveData += strName1 + '--' + str(timeInterval) + '\n'
            strName1 = strName2 = ''
            
LoadData('filetest.txt')
now_time = datetime.datetime.now()
time_str = datetime.datetime.strftime(now_time,'%Y%m%d_%H_%M_%S')#时间转字符串
outfilepath = time_str + '_logAnalysis.txt'
outfilepath = 'test.txt'
with open(outfilepath,'w') as saveFile:#写入数据到文件
    saveFile.write(allSaveData)
print ('end')



读写文件

# -*- coding:utf-8 -*-
print(r'read()会一次性读取文件的全部内容')
with open('./test.txt', 'r') as f:
    print(f.read())

print(r'调用read(size)方法,每次最多读取size个字节的内容')
with open('./test.txt', 'r') as f:
    while True:
        tmpdata = f.read(10)
        if tmpdata:
            print(tmpdata)
        else:
            break

print(r'调用readlines()一次读取所有内容并按行返回list')
with open('./test.txt', 'r') as f:
    for line in f.readlines():
        print(line.strip())  # 把末尾\n删除

'''
1.读取二进制文件--open('./test.png', 'rb')
2.读取指定字符编码--open('./test.txt', 'r', 
                        encoding='gbk', errors='ignore')
'''
# 写文件 'w'-覆盖已文件;'a'-追加
with open('./test.txt', 'w') as f:
    f.write('hello,gg!')



版权声明:本文为yuxing55555原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。