diff --git a/combine_files.py b/combine_files.py index 819dcf0..f971ad3 100644 --- a/combine_files.py +++ b/combine_files.py @@ -31,15 +31,22 @@ def cleanRow(row): for listing in raw_files: print(listing) if os.path.isfile(listing): - file_date = re.match('.*([0-9]{8}).*csv$', listing).group(1) - with open(listing) as csvfile: - spamreader = csv.reader(csvfile) - data = [] - for row in spamreader: - row = cleanRow(row) - data.append(dict(zip(header, row))) - - full_data[file_date] = data + # Updated regex to match 'M.YYYY' format in the filename + match = re.match(r'.*([0-9]{1,2}\.[0-9]{4}).*\.csv$', listing) + + # Check if match exists before accessing group(1) to avoid AttributeError + if match: # Updated conditional check to prevent errors + file_date = match.group(1) + with open(listing) as csvfile: + spamreader = csv.reader(csvfile) + data = [] + for row in spamreader: + row = cleanRow(row) + data.append(dict(zip(header, row))) + + full_data[file_date] = data + else: + print(f"Warning: No valid date found in filename '{listing}'") # Added warning message with open(combined_file, 'w') as jsonFile: json.dump(full_data, jsonFile, indent=4)