1 需求
测试需要抓取audio相关的参数,遍历log,找到需要的参数并存入表格
2 结果
usecase_result文件夹存放log
0_xxx.bat用于运行python文件,就一句python xxx.py
2.1 0_xxx.bat
@echo off
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
set "DEL=%%a"
)
@echo ------------------- python -----------------------
python xxx.py
@echo ------------------- EXIT ---------------------------
echo=
pause
exit
2.2 key_word.csv
第一行是参数名称,只是用于表示当前一列内容是什么
第二行是搜索的关键字,遍历log时查找是否包含该内容,若包含则进行参数提取
第三行是参数前的关键字
第四行是参数后的关键字
以usecase为例
01-08 22:06:47.478 642 2504 D audio_hw_primary: select_devices for use case (low-latency-playback)
这一句log我需要提取的是黄色内容,如上“case (”是参数前关键字,“)”是参数后的关键字
“select_devices for use case”是该行的搜索关键字
注:可以在该csv文件中继续按列添加
如果参数后是换行符,则后关键字是‘\n’
2.3 xxx.py
import os
import csv
import numpy as np
import json
def get_front_2_last (line, front, last):
begin=line.find(front)+len(front)
word=line[begin:]
end=word.find(last)
# print("begin=",begin,"end=",end)
ss=word[:end]
# print(ss)
return ss
def apply_file (file_name,num,row_x):
f_log=open(file_name,"r",encoding='utf-8')
line_log=f_log.readline()
# set result[] to save value
result_x=[]
x=0
for x in range(num):
result_x.append('')
while line_log:
temp=0
for key in rows[1]:
a=key in line_log
if True == a :
key_val=get_front_2_last(line_log,rows[2][temp],rows[3][temp])
# print("key_val=",key_val)
temp_x= key_val in result_x[temp]
if False == temp_x :
result_x[temp]=result_x[temp]+key_val+'\n'
temp=temp+1
line_log=f_log.readline() #read the next line
f_log.close()
x=0
for x in range(num):
temp=result_x[x]
temp=temp.strip()
result_x[x]=temp
# zhuijia 0 weizhi zifuchuan
result_x.insert(0,file_name)
with open("test.csv","a+", newline='\n') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(result_x)
# writer.writerow(['a','b'])
#写入多行用writerows
# writer.writerows([[0,1,3],[1,2,3],[2,3,4]])
csvfile.close()
return True
# get key word
with open('key.csv','r') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
csvfile.close()
# global n
(m, n) = np.shape(rows)
# save headline
row_temp=rows[0]
row_temp.insert(0,'file_name')
with open("test.csv","w", newline='\n') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(row_temp)
csvfile.close()
# resolve file dir
resultdir="usecase_result"
for root, dirs, files in os.walk(resultdir):
for f in files:
temp='.txt' in f
if temp == True :
file_path=root
log_file=os.path.join(root, f) #log_file path\filename
print(log_file)
apply_file(log_file,n,rows[0])
2.5 检索结果
3 小结
真的超级好用(陈婆卖瓜),后续测试和写app就方便很多,嘿嘿