从日志读取关键数据,按照相关日期进行数据分析

分析靠近后向挡墙的距离

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.")

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-24 15:26:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-24 15:26:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-24 15:26:04       82 阅读
  4. Python语言-面向对象

    2024-04-24 15:26:04       91 阅读

热门阅读

  1. 链接备份记录

    2024-04-24 15:26:04       66 阅读
  2. c++多态

    c++多态

    2024-04-24 15:26:04      29 阅读
  3. Ubuntu中如何压缩和解压文件

    2024-04-24 15:26:04       35 阅读
  4. JVM(1)

    2024-04-24 15:26:04       181 阅读
  5. 物联网社区信息化管理系统设计的毕业论文

    2024-04-24 15:26:04       88 阅读
  6. 面试 Python 基础八股文十问十答第五期

    2024-04-24 15:26:04       197 阅读
  7. intellij idea的快速配置详细使用

    2024-04-24 15:26:04       37 阅读
  8. Python实现深度学习

    2024-04-24 15:26:04       28 阅读
  9. 深入浅出MySQL-03-【MySQL中的运算符】

    2024-04-24 15:26:04       25 阅读
  10. System1和System2

    2024-04-24 15:26:04       32 阅读
  11. Android如何管理多进程

    2024-04-24 15:26:04       38 阅读
  12. 经典的目标检测算法

    2024-04-24 15:26:04       26 阅读
  13. python实现DIY高考倒计时小软件

    2024-04-24 15:26:04       31 阅读