脚本同路径保存需要筛选的log信息,然后修改脚本中文件名,运行即可

 

# -*-encoding:utf-8 -*-
"""
@Time 2023/6/9 17:15
@Auth ruqing
@File eeprom相关参数查询.py
@IDE PyCharm
@MottoABC(Always Be Coding)
"""

import re
import openpyxl

# 打开日志文件和Excel文件
log_file = open( "反复复位172.21.120.209--7025--250-8.log", "r" )
wb = openpyxl.Workbook()
ws = wb.active

# 写入Excel文件的标题行
ws.append(
["Port", "Temperature", "Vcc", "RX1Power", "RX2Power", "RX3Power", "RX4Power", "RX5Power", "RX6Power", "RX7Power",
"RX8Power", "TX1Bias", "TX2Bias", "TX3Bias", "TX4Bias", "TX5Bias", "TX6Bias", "TX7Bias", "TX8Bias",
"TX1Power", "TX2Power", "TX3Power", "TX4Power", "TX5Power", "TX6Power", "TX7Power", "TX8Power"] )

# 定义正则表达式来解析日志中的端口号和值
port_regex = r"Ethernet(\d+)"
temp_regex = r"Temperature:\s+(\d+\.\d+)C"
voltage_regex = r"Vcc:\s+(\d+\.\d+)Volts"
value_regex = r"\s+(RX|TX)(\d+)(Power|Bias):\s+([-.\d]+)"
"""
\s+ 匹配一个或多个空格
(RX|TX) 匹配"RX""TX"
(\d+) 匹配一个或多个数字
(Power|Bias) 匹配"Power""Bias"
: 匹配一个冒号
[-.\d]+ 匹配一个或多个数字、连字符或小数点
"""

# 定义变量来存储当前端口的通道监视值
values = {}

# 逐行读取日志文件,解析每个端口的值,并将其写入Excel文件
for line in log_file:
match = re.search( port_regex, line )
if match:
# 如果找到了一个新的端口,则将上一个端口的值写入Excel文件,并清空变量
if values:
values_row = [values.get( "Temperature", "" ), values.get( "Vcc", "" )]
values_row += [values.get( f"RX{i}Power", "" ) for i in range( 1, 9 )]
values_row += [values.get( f"TX{i}Bias", "" ) for i in range( 1, 9 )]
values_row += [values.get( f"TX{i}Power", "" ) for i in range( 1, 9 )]
ws.append( [('Ethernet') + port] + values_row )
values.clear()
# 解析当前端口号
port = match.group( 1 )
else:
# 解析当前行中的值
match = re.search( temp_regex, line )
if match:
values["Temperature"] = match.group( 1 )
else:
match = re.search( voltage_regex, line )
if match:
values["Vcc"] = match.group( 1 )
else:
match = re.search( value_regex, line )
if match:
channel = match.group( 1 ) + match.group( 2 )
key = channel + match.group( 3 )
value = match.group( 4 )
if key.startswith( "RX" ):
values[key] = value
elif key.startswith( "TX" ):
values[key] = value
# 处理最后一个端口的值
if values:
values_row = [values.get( "Temperature", "" ), values.get( "Vcc", "" )]
values_row += [values.get( f"RX{i}Power", "" ) for i in range( 1, 9 )]
values_row += [values.get( f"TX{i}Bias", "" ) for i in range( 1, 9 )]
values_row += [values.get( f"TX{i}Power", "" ) for i in range( 1, 9 )]
ws.append( [('Ethernet') + port] + values_row )

# 保存Excel文件
wb.save( "参数信息--反复复位7025.xlsx" )
print( '参数查询成功' )
posted on 2023-06-09 20:45  Star*S  阅读(85)  评论(0编辑  收藏  举报