用简单的Pyhton脚本读取Log信息并绘图(Python3.7)

最近公司开发一款新产品,调试信息通过Jlink-RTT打印出来,存储在一个log文件里(记事本就能打开的那种)。其中有四路温度传感器的数值需要人工读数,输进Excel,然后画时间曲线。我寻思这log文件都有了还人工读数,太浪费生产力了吧。于是掏出python,几行代码解决问题。

首先来看打印出的log数据格式:

0> Voltage: pwr = 1.38V, vin = 3.87V, temp1 = 1.54V, temp2 = 1.69V, temp3 = 1.69V, temp4 = 1.69V, temp5 = 1.66V water1 = 0.04V, water2 = 0.03V!
 0> POWER: vin voltage = 3.87V, *flag = 0, *percent = 60%!
 0> TEMP:  
 0> temp1(heat1) = 26.02oC,
 0> temp2(heat2) = 32.35oC, 
 0> temp3(cavity1) = 30.98oC,
 0> temp4(cavity2) = 32.25oC, 
 0> temp5(pcb) = 30.79oC, *flag1 = 0, *flag2 = 0, *flag3 = 0

其中temp1(heat1)等四行数据就是我们需要提取的数据。这个格式非常规整,甚至不需要正则表达式就能提取。我的思路是遍历文件,找到含有temp1(heat1)的行,直接提取其中第19~24位的数据即可(人工数出来的。。。)

开整!

首先因为要生成温度曲线,需要引入pylab库。此库安装方法是

pip install matplotlib

速度比较慢。。。可以去github或csdn上找找适合自己系统的版本下载。

首先定义几个列表,用于存储找到的数据。

    y1 = []
    y2 = []
    y3 = []
    y4 = []
    x1 = []
    x2 = []
    x3 = []
    x4 = []

y轴是温度信息,x轴是时间信息。由于设定log温度信息每1s打印一次,所以x轴信息可以通过读取y轴列表长度来获取,这是后话。接下来是定义文件位置并且遍历文件:

Syslog = "D:/7.log"
    with open(Syslog,'r')as f:
        for line in f.readlines():
            try:
                if'temp1(heat1) =' in line:
                    #print(line[19:24])
                    y1.append(float(line[19:24]))
                if'temp2(heat2) =' in line:
                    #print(line[19:24])
                    y2.append(float(line[19:24]))
                if'temp3(cavity1) =' in line:
                    #print(line[21:26])
                    y3.append(float(line[21:26]))
                if'temp4(cavity2) =' in line:
                    #print(line[21:26])
                    y4.append(float(line[21:26]))
            except:
                print('erro')

你问我为什么后面两个数据变成了提取21~26?因为temp3(cavity1)比temp1(heat1)长啊。。。记得要对采集到的数据信息强制转换为float型,不然得到的数据是str类型没法画图的。如果没有提取到数据就打印erro。

然后打印出y轴的所有数据,方便复制进excel给老板看(我寻思他直接看plot出来的不行么)

        print('T1:')
        print('************************************')
        for i in range(len(y1)):
            print(y1[i])
        print('T2:')
        print('************************************')
        for i in range(len(y2)):
            print(y2[i])
        print('C1:')
        print('************************************')
        for i in range(len(y3)):
            print(y3[i])
        print('C2:')
        print('************************************')
        for i in range(len(y4)):
            print(y4[i])

然后就是我们之前提到的获取时间轴信息,直接读取y列表长度即可。

        x1 = range(0,len(y1))
        x2 = range(0,len(y2))
        x3 = range(0,len(y3))
        x4 = range(0,len(y4))

现在可以通过plot进行绘图了

        pylab.figure(1)
        pylab.plot(x1,y1,'r',label = 'tempheat1')
        pylab.plot(x2,y2,'b',label = 'tempheat2')
        pylab.plot(x3,y3,'g',label = 'tempC1')
        pylab.plot(x4,y4,'y',label = 'tempC2')
        pylab.show()

打印数据结果(可以直接复制进excel):

绘图结果:

附完整代码:

import pylab

def get_number():
    y1 = []
    y2 = []
    y3 = []
    y4 = []
    x1 = []
    x2 = []
    x3 = []
    x4 = []
    Syslog = "D:/7.log"
    with open(Syslog,'r')as f:
        for line in f.readlines():
            try:
                if'temp1(heat1) =' in line:
                    #print(line[19:24])
                    y1.append(float(line[19:24]))
                if'temp2(heat2) =' in line:
                    #print(line[19:24])
                    y2.append(float(line[19:24]))
                if'temp3(cavity1) =' in line:
                    #print(line[21:26])
                    y3.append(float(line[21:26]))
                if'temp4(cavity2) =' in line:
                    #print(line[21:26])
                    y4.append(float(line[21:26]))
            except:
                print('erro')
        print('T1:')
        print('************************************')
        for i in range(len(y1)):
            print(y1[i])
        print('T2:')
        print('************************************')
        for i in range(len(y2)):
            print(y2[i])
        print('C1:')
        print('************************************')
        for i in range(len(y3)):
            print(y3[i])
        print('C2:')
        print('************************************')
        for i in range(len(y4)):
            print(y4[i])
        x1 = range(0,len(y1))
        x2 = range(0,len(y2))
        x3 = range(0,len(y3))
        x4 = range(0,len(y4))
        pylab.figure(1)
        pylab.plot(x1,y1,'r',label = 'tempheat1')
        pylab.plot(x2,y2,'b',label = 'tempheat2')
        pylab.plot(x3,y3,'g',label = 'tempC1')
        pylab.plot(x4,y4,'y',label = 'tempC2')
        pylab.show()
                
if __name__ == '__main__':
    get_number()

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值