代码如下:

# -*- coding:utf-8 -*-
# @author:Ye Zhoubing
# @datetime:2025/8/18 14:04
# @software: PyCharm
"""
按小时匹配多个 CSV 文件中的数据,并将结果保存为新的 Excel 文件。
"""
import pandas as pd
import os# CSV 文件夹
folder_path = r'C:\Users\32649\Desktop\excel\water-discharge\csv2\2月-3月'
output_path = "all_files_common_times_2-3月.xlsx"# 允许误差 ±20 分钟
tolerance = pd.Timedelta(minutes=20)# 读取所有 csv 的时间列
time_lists = []
file_names = [f for f in os.listdir(folder_path) if f.endswith(".csv")]def load_time_series(file_path):df = pd.read_csv(file_path, encoding="gbk")# 自动识别时间列名time_col = Nonefor col in df.columns:if "date" in col.lower() or "时间" in col:time_col = colbreakif time_col is None:raise ValueError(f"{file_path} 没有找到时间列")df[time_col] = pd.to_datetime(df[time_col])return df[time_col].sort_values().reset_index(drop=True)# 读取所有文件的时间序列
for f in file_names:path = os.path.join(folder_path, f)time_lists.append(load_time_series(path))# 以第一个文件的时间作为基准
common_times = []
for t in time_lists[0]:ok = Truealigned = [t]  # 保存每个文件匹配到的时间for other_times in time_lists[1:]:diffs = abs(other_times - t)min_diff = diffs.min()if min_diff <= tolerance:aligned.append(other_times.loc[diffs.idxmin()])else:ok = Falsebreakif ok:common_times.append(aligned)# 保存结果
result = pd.DataFrame(common_times, columns=file_names)
result.to_excel(output_path, index=False)print(f"匹配完成,共找到 {len(result)} 个共同时间,已保存到 {output_path}")