从日志读取关键数据,按照相关日期进行数据分析
分析靠近后向挡墙的距离
import os
import re
import sys
import matplotlib.pyplot as plt
from datetime import datetime
def process_distance_data(file_path):
distances = []
timestamps = []
try:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
for line in file:
match = re.search(r'\[(\d{4}-\d{2}-\d{2}),(\d{2}:\d{2}:\d{2}\.\d{3})\]-距离后向挡墙距离([\d.]+) 完整度:([\d.]+)', line)
if match:
date_str = match.group(1)
time_str = match.group(2)
timestamp_str = f"{date_str},{time_str}"
timestamp = datetime.strptime(timestamp_str, '%Y-%m-%d,%H:%M:%S.%f')
distance_str = match.group(3)
distance = float(distance_str) if distance_str != "340282346638528859811704183484516925440" else 80
if distance <= 80:
distances.append(distance)
timestamps.append(timestamp)
except FileNotFoundError:
print(f"Error: File '{file_path}' not found.")
except Exception as e:
print(f"Error: {e}")
return distances, timestamps
def visualize_distances(distances, timestamps):
plt.scatter(timestamps, distances, color='red', label='Data Points') # 红色点
plt.plot(timestamps, distances, color='blue', label='Line Plot') # 蓝色折线
plt.title('Distance from Rear Wall')
plt.xlabel('Timestamp')
plt.ylabel('Distance')
plt.xticks(rotation=45)
plt.legend() # 添加图例
plt.tight_layout()
plt.show()
def export_distances_to_txt(distances, output_file):
with open(output_file, 'w') as file:
for distance in distances:
file.write(f"{distance}\n")
print(f"Distances exported to {output_file}")
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python script.py <file_path>")
sys.exit(1)
file_path = sys.argv[1]
output_file = "distances.txt"
if os.path.exists(file_path):
distances, timestamps = process_distance_data(file_path)
visualize_distances(distances, timestamps)
# No need to export to txt file since you didn't provide the export_distances_to_txt function
else:
print("File not found.")
原文地址:https://blog.csdn.net/CCCrunner/article/details/137875751
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!