本文主要是介绍使用Python提取日志数据并绘制波形图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装库
pip install matplotlib numpy
代码
import re
import numpy as np
import matplotlib.pyplot as plt# 读取日志文件
file_path = 'desktop\123.DAT' # 文件路径with open(file_path, 'r') as file:log_text = file.read() # 读取文件内容# 提取AverDIFF的值
data = []
pattern = r"AverDIFF:(-?\d+)" # 匹配AverDIFF的值,包括负数
matches = re.findall(pattern, log_text)# 将提取的值转换为整数并存储在列表中
for match in matches:data.append(int(match))# 总数据个数
total_count = len(data)
print(f"总的数据个数: {total_count}")# 过滤异常数据并打印数量
filtered_data = [d for d in data if d < -2000 or d > 2000]
filtered_count = len(filtered_data)
print(f"异常数据个数: {filtered_count}")
if filtered_count > 0:print(f"异常数据: {filtered_data}")# 打印正常数据个数
filtered_data_array = [d for d in data if d not in filtered_data]
filtered_data_array_count = len(filtered_data_array)
print(f"正常数据个数: {filtered_data_array_count}")# 如果没有提取到符合条件的数据,直接返回
if not filtered_data_array:print("没有找到符合条件的AverDIFF数据")
else:# 将过滤后的数据转换为numpy数组filtered_data_array = np.array(filtered_data_array)# 绘制波形图plt.figure(figsize=(12, 6)) # 设置图表大小plt.plot(filtered_data_array, marker='o') # 使用点连接线plt.title("Filtered AverDIFF Waveform") # 图表标题plt.xlabel("Index") # x轴标签plt.ylabel("AverDIFF Value") # y轴标签plt.xlim(0, len(filtered_data_array) + 10) # 设置X轴范围plt.grid()plt.show() # 显示图表
这篇关于使用Python提取日志数据并绘制波形图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!