From a6faf139cb6b165ced3c04ec5b9a935dd2ce7e87 Mon Sep 17 00:00:00 2001 From: AnshulLakhera Date: Mon, 28 Oct 2024 10:30:20 +0530 Subject: [PATCH] Updated combined_files.py --- combine_files.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) 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)