python_统计每秒的时间戳的次数

统计文件中的时间戳,每秒出现的次数;

from collections import defaultdict
import csv


def count_timestamps_per_second(filename):
    timestamp_counts = defaultdict(int)
    with open(filename, 'r') as file:
        reader1 = csv.reader(file, delimiter=',')
        data1 = list(reader1)
        for line in data1:
            try:
                microsecond_timestamp = int(float(line[-2]))
                timestamp_counts[microsecond_timestamp] += 1
            except ValueError:
                print(f"无法解析时间戳:{line[-2]}")
                continue
    return timestamp_counts


# 文件每行的内容如下:30.5356248,114.3516616,22.0851853,8.5900784,-5.4975657,-0.1361635,0.0118782,-0.0347502,2.1172120,0.0095332,-0.0012775,-0.0117969,0.3363853,-0.1735796,9.8258036,0.3162913,0.3162913,0.3162913,0.3162379,0.3162379,0.3162379,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,1695109430.7060649,1713859089.3673823
filename = r'E:\DPI\台架测试\V1测试\2023-12-07-17-32-13_tunnel隧道\fused_location_202404230757.txt'
second_wise_counts = count_timestamps_per_second(filename)

# 打印结果
for second, count in sorted(second_wise_counts.items()):
    print(f"时间戳 {second} 秒内有 {count} 个时间戳")

基于以上,再比较文件同一时刻的差异,并写入文件;

import os
from collections import defaultdict
import csv
from datetime import datetime


def count_timestamps_per_second(filename):
    timestamp_counts = defaultdict(int)
    with open(filename, 'r') as file:
        reader1 = csv.reader(file, delimiter=',')
        data1 = list(reader1)
        for line in data1:
            try:
                microsecond_timestamp = int(float(line[-2]))
                timestamp_counts[microsecond_timestamp] += 1
            except ValueError:
                print(f"无法解析时间戳:{line[-2]}")
                continue
    return timestamp_counts


filename_m = r'E:\DPI\台架测试\V2.1.1测试\yutong_brt_all\msfl_result_202405200256.txt'
second_wise_counts_m = count_timestamps_per_second(filename_m)
filename_r = r'E:\DPI\台架测试\V2.1.1测试\yutong_brt_all\raw_ins_202405200256.txt'
second_wise_counts_r = count_timestamps_per_second(filename_r)

# # 打印结果
# for second_m, count_m in sorted(second_wise_counts_m.items()):
#     # 频率少于100
#     if count_m < 100:
#         print(f"{second_m} 秒内有 {count_m} 个时间戳")
# print("**********************")
# for second, count in sorted(second_wise_counts_r.items()):
#     # 频率少于100
#     if count < 100:
#         print(f"{second} 秒内有 {count} 个时间戳")

# 起始时间戳
print("起始时间戳:", next(iter(second_wise_counts_r.keys())))
# 截止时间戳
print("截止时间戳:", next(reversed(second_wise_counts_r)))

# 存放文件
file_dir = os.path.dirname(filename_r)
t = datetime.now().strftime("%m%d%H%M")
target_path = os.path.join(file_dir, 'raw&msfl_差异' + t + '.txt')

with open(target_path, 'w') as resultfile:
    # 逐行比较
    for sedond, count in sorted(second_wise_counts_r.items()):
        for second_m, count_m in sorted(second_wise_counts_m.items()):
            if second_m == sedond:
                if count_m - count == 0:
                    # 该时间点不存在差异,退出循环
                    break
                else:
                    result = str(second_m) + " 存在差异,该秒内原始数据有" + str(count) + "个,融合后有" + str(
                        count_m) + "个"
                    print(result)
                    resultfile.writelines(result + "\n")

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值