2026-02-09 11:36:37 +08:00
|
|
|
|
# 读取原始数据文件
|
|
|
|
|
|
import datetime
|
2025-12-12 18:00:14 +08:00
|
|
|
|
|
2026-03-13 21:04:19 +08:00
|
|
|
|
input_file = r"C:\Users\xj-robot\Desktop\weight.txt" # 原始数据文件路径
|
|
|
|
|
|
output_file = r"C:\Users\xj-robot\Desktop\filtered_B_records.txt" # 输出结果文件路径 (使用原始字符串避免Unicode转义错误)
|
2025-12-12 18:00:14 +08:00
|
|
|
|
|
2026-02-09 11:36:37 +08:00
|
|
|
|
# 存储包含"B"标记的记录
|
|
|
|
|
|
filtered_records = []
|
|
|
|
|
|
|
|
|
|
|
|
# 读取并过滤数据
|
|
|
|
|
|
with open(input_file, "r", encoding="utf-8") as f:
|
|
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
for line in lines:
|
|
|
|
|
|
# 筛选包含"B,"的行(避免误匹配其他含B的字符)
|
|
|
|
|
|
if "B," in line or "F," in line:
|
|
|
|
|
|
# 移除等号
|
|
|
|
|
|
cleaned_line = line.strip().replace('=', '')
|
|
|
|
|
|
|
|
|
|
|
|
# 分割行数据
|
|
|
|
|
|
parts = cleaned_line.split(',')
|
|
|
|
|
|
|
|
|
|
|
|
if len(parts) >= 2:
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 解析前两个日期(假设格式为YYYY-MM-DD或YYYY/MM/DD)
|
|
|
|
|
|
date1_str = parts[0].strip()
|
|
|
|
|
|
date2_str = parts[1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
# 尝试不同的日期格式
|
|
|
|
|
|
date_formats = ['%Y-%m-%d %H:%M:%S', '%Y-%m-%d', '%Y/%m/%d']
|
|
|
|
|
|
date1 = None
|
|
|
|
|
|
date2 = None
|
|
|
|
|
|
|
|
|
|
|
|
for fmt in date_formats:
|
|
|
|
|
|
try:
|
|
|
|
|
|
if date1 is None:
|
|
|
|
|
|
date1 = datetime.datetime.strptime(date1_str, fmt)
|
|
|
|
|
|
if date2 is None:
|
|
|
|
|
|
date2 = datetime.datetime.strptime(date2_str, fmt)
|
|
|
|
|
|
except ValueError:
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
if date1 and date2:
|
|
|
|
|
|
# 计算时间差(秒数)
|
|
|
|
|
|
total_seconds = (date2 - date1).total_seconds()
|
|
|
|
|
|
# 转换为分:秒格式
|
|
|
|
|
|
minutes = int(total_seconds // 60)
|
|
|
|
|
|
seconds = int(total_seconds % 60)
|
|
|
|
|
|
time_diff = f"{minutes:02d}:{seconds:02d}"
|
|
|
|
|
|
# 在行末尾添加时间差
|
|
|
|
|
|
cleaned_line = f"{cleaned_line},{time_diff}"
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
# 如果日期解析失败,保持原始行不变
|
|
|
|
|
|
pass
|
2025-12-12 18:00:14 +08:00
|
|
|
|
|
2026-02-09 11:36:37 +08:00
|
|
|
|
filtered_records.append(cleaned_line)
|
|
|
|
|
|
|
|
|
|
|
|
# 保存过滤后的数据
|
|
|
|
|
|
with open(output_file, "w", encoding="utf-8") as f:
|
|
|
|
|
|
f.write("\n".join(filtered_records))
|
|
|
|
|
|
|
|
|
|
|
|
print(f"过滤完成!共提取到 {len(filtered_records)} 条含'B'标记的记录")
|
|
|
|
|
|
print(f"结果已保存至:{output_file}")
|