import os import csv # Function to parse key-value pairs from a .dat file def parse_dat_file(file_path): attributes = {} with open(file_path, 'r') as file: for line in file: line = line.strip() # Remove whitespace if "=" in line: # Only process lines with key-value pairs key, value = line.split("=", 1) attributes[key.strip()] = value.strip() return attributes # Function to combine .dat files into a single CSV def combine_dat_files_to_csv(): current_dir = os.getcwd() # Get current directory csv_file_path = os.path.join(current_dir, 'combined_output.csv') # Find all .dat files in the current directory dat_files = [f for f in os.listdir(current_dir) if f.endswith('.dat')] if not dat_files: print("No .dat files found in the current directory.") return # Open the CSV file for writing with open(csv_file_path, 'w', newline='') as csv_file: csv_writer = csv.writer(csv_file) # Write CSV header (combine all unique keys across files) headers = set() parsed_files = [] for dat_file in dat_files: dat_file_path = os.path.join(current_dir, dat_file) parsed_data = parse_dat_file(dat_file_path) headers.update(parsed_data.keys()) parsed_files.append((dat_file, parsed_data)) headers = ["FileName"] + sorted(headers) # Add FileName as the first column csv_writer.writerow(headers) # Write rows for each .dat file for file_name, attributes in parsed_files: row = [file_name] + [attributes.get(header, "") for header in headers[1:]] csv_writer.writerow(row) print(f"Combined output saved to {csv_file_path}") # Run the function to combine .dat files into a CSV combine_dat_files_to_csv()