import re
log = "[2020-Feb-28 09:44:59.111749] [INFO] [CHASSIS_DRIVE] [2020-02-28 09:44:44.655924] dif_pos=(13.0083,-0.00512981,-0.00623378) loading=(-363,436) drv_utliz=(155,147) obstacle=(0,0) cur_twist=(-1.27467,-0.00245437) tar_twist=(-1.28842,0.00343069)"
x = re.findall(r'dif_pos=\(([^,]+),[^,]+,[^)]+\)', log,re.M|re.I)
>>> x
['13.0083']
>>>
对 x = re.findall(r'dif_pos=\(([^,]+),[^,]+,[^)]+\)', log,re.M|re.I)的解释:
r 代表
# coding:utf8
import re
import pandas as pd
def load_log():
try:
file_object = open('industry.log_20200910-165620.1156')
file_context = file_object.read()
finally:
file_object.close()
return file_context
def launcher_log(content):
#wanted = re.findall(r'[A-Za-z]\d{4}\s.*\]\sD:\\jinxin\\[0-9]\\([0-9]-Platform[AB]-[0-9]-[0-9])\.bmp,result\s[0-9]\s,center\s\[(\d{4},\d{4}\.\d{2})\]\s,radius\s([0-9]{3,}\.[0-9]{3,}),score\s([0-9]+\.[0-9]{6,})',content, re.M|re.I)
Platform = re.findall(r'[A-Za-z]\d{4}\s.*\]\sD:\\jinxin\\[0-9]\\([0-9].*-Platform[AB]-[0-9]{1,3}-[0-9])\.bmp',content,re.M|re.I)
Center = re.findall(r'center\s\[([0-9]{3,4}\.?[0-9]{1,4}?,[0-9]{3,4}\.[0-9]{1,4})\]',content,re.M)
Radius =re.findall(r'radius\s([0-9]{1,4}\.[0-9]{1,4})',content,re.M)
Score = re.findall(r'score\s(0\.[1-9]{1,9})',content,re.M)
#将多个List合并转化为dataframe:
df_draft = pd.DataFrame(Platform, columns=['Platform'])
df_draft = pd.concat([df_draft, pd.DataFrame(Center,columns=['Center'])],axis=1)
df_draft = pd.concat([df_draft, pd.DataFrame(Radius,columns=['Radius'])],axis=1)
df_draft = pd.concat([df_draft, pd.DataFrame(Score,columns=['Score'])],axis=1)
#按score排序
#参考官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.sort_values.html
df_industry = df_draft.sort_values(by=['Score'])
#需要pip install openpyxl
df_industry.to_excel("Platform_score.xlsx", index=False)
print(df_industry)
if __name__ == "__main__":
# 读日志
content = load_log()
# 使用正则表达式提取日志,并导出在excel
launcher_log(content)
# 分析问题