|
|
发表于 2025-10-8 21:52:17
From FishC Mobile
|
显示全部楼层
import csv
from pathlib import Path
folder = Path("attendance")
out_file = Path("merged.csv")
#增加筛选条件的自定义配置项(后续改条件直接动这里)
target_col = "direction" # 要判断的指定列名
target_value = "OUT" # 指定列需满足的条件值
# 初始化指定列的索引变量(用于记录列在表头中的位置)
target_col_index = -1
rows = []
header = None
for f in folder.iterdir():
if f.is_file() and f.suffix.lower() == ".csv" and f.name != out_file.name:
with f.open("r", newline="", encoding="utf-8", errors="ignore") as rf:
reader = csv.reader(rf)
h = next(reader, None)
if h is None:
continue
if header is None:
header = h
# 校验指定列是否存在,并记录其索引
if target_col not in header:
print(f"错误:表头中未找到指定列 '{target_col}',程序退出。")
exit() # 列不存在则直接退出,避免后续报错
target_col_index = header.index(target_col) # 记录指定列的位置
elif header != h:
print(f"警告:{f} 的表头与前者不一致,跳过。")
continue
# 原“直接追加所有行”逻辑,改为“先筛选再追加”
for row in reader:
# 容错处理:避免行数据不完整导致索引越界
if len(row) <= target_col_index:
print(f"警告:{f} 中某行数据长度不足,跳过该行。")
continue
# 核心筛选条件:仅保留指定列等于目标值的行
if row[target_col_index] == target_value:
rows.append(tuple(row)) # 满足条件才追加,不满足则跳过
# 整行去重
unique_rows = list(dict.fromkeys(rows))
if header:
with out_file.open("w", newline="", encoding="utf-8") as wf:
writer = csv.writer(wf)
writer.writerow(header)
writer.writerows(unique_rows)
# 输出提示补充“筛选”关键词
print(f"完成,合并并筛选后共 {len(unique_rows)} 条记录 -> {out_file}")
else:
print("未找到可合并的CSV。") |
|