最近公司開發一款新產品,調試信息通過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()