专题4:按车辆ID筛选后将不同文件夹下相同名称的csv文件进行合并_ngsim教程-优快云博客https://blog.youkuaiyun.com/weixin_50632459/article/details/124746073用这个代码没跑出来,用ai改了代码,成功了,分享代码如下:
import pandas as pd
import os
import csv
import time
class FileProcessor:
def __init__(self, source_path, target_path):
self.source_path = source_path
self.target_path = target_path
self.file_index = {}
def collect_files(self):
"""
一次性收集所有 CSV 文件和路径,并根据车辆 ID 组织。
"""
for root, _, files in os.walk(self.source_path):
for file in files:
if file.endswith('.csv'):
vehicle_id = self.extract_vehicle_id(file)
if vehicle_id is not None:
self.file_index.setdefault(vehicle_id, []).append(os.path.join(root, file))
def extract_vehicle_id(self, filename):
"""
提取文件名中的车辆 ID。
"""
try:
return int(filename.split('.')[0]) # 假设文件名以“车辆ID.csv”格式命名
except ValueError:
return None # 跳过无法提取 ID 的文件
def process_files(self):
"""
创建目标路径、合并文件,并删除空文件夹。
"""
os.makedirs(self.target_path, exist_ok=True)
for vehicle_id, file_paths in self.file_index.items():
vehicle_folder = os.path.join(self.target_path, f"Vehicle_{vehicle_id}")
os.makedirs(vehicle_folder, exist_ok=True)
combined_file = os.path.join(vehicle_folder, f"{vehicle_id}_combined.csv")
self.combine_files(file_paths, combined_file)
def combine_files(self, file_paths, output_file):
"""
合并多个 CSV 文件到一个目标文件。
"""
columns = ['Vehicle_ID', 'Frame_ID', 'Total_Frames', 'Global_Time', 'Local_X', 'Local_Y',
'Global_X', 'Global_Y', 'v_length', 'v_Width', 'v_Class', 'v_Vel', 'v_Acc',
'Lane_ID', 'O_Zone', 'D_Zone', 'Int_ID', 'Section_ID', 'Direction', 'Movement',
'Preceding', 'Following', 'Space_Headway', 'Time_Headway', 'Location']
with open(output_file, "w", newline="", encoding="utf-8") as out_file:
writer = csv.writer(out_file)
writer.writerow(columns) # 写入表头
for file_path in file_paths:
try:
df = pd.read_csv(file_path)
df.to_csv(out_file, header=False, index=False, mode="a")
except Exception as e:
print(f"合并时出错,跳过文件:{file_path}, 错误信息:{e}")
def delete_empty_folders(self, path):
"""
删除空文件夹。
"""
for root, dirs, files in os.walk(path, topdown=False):
for dir_name in dirs:
dir_path = os.path.join(root, dir_name)
if not os.listdir(dir_path):
os.rmdir(dir_path)
def run(self):
start_time = time.time()
print("开始收集文件...")
self.collect_files()
print(f"文件收集完成,用时:{time.time() - start_time:.2f} 秒")
print("开始处理文件...")
self.process_files()
print(f"文件处理完成,用时:{time.time() - start_time:.2f} 秒")
print("清理空文件夹...")
self.delete_empty_folders(self.target_path)
print(f"清理完成,用时:{time.time() - start_time:.2f} 秒")
if __name__ == "__main__":
# 设置路径
source_path = r"C:\Users\DELL\Desktop\NGSIM_data_processing\split_NGSIM_Data\veh_selection_NGSIM_Data_1"
target_path = r"C:\Users\DELL\Desktop\NGSIM_data_processing\split_NGSIM_Data\veh_combine_NGSIM_Data_2"
# 实例化并运行
processor = FileProcessor(source_path, target_path)
processor.run()