Csvimport
unknown
plain_text
a year ago
1.0 kB
13
Indexable
import os
import re
import csv
# Initialize an empty dictionary
br_dict = {}
# Define the regex pattern to match the folder names
pattern = re.compile(r'^BR\d+(_STR\d+)?$')
# Loop through all folders in the current directory
for folder in os.listdir('.'):
if os.path.isdir(folder) and pattern.match(folder):
# Extract the BR number and STR number if present
br_number = re.search(r'^BR\d+', folder).group(0)
str_number = re.search(r'STR\d+', folder)
if str_number:
str_number = str_number.group(0)
else:
str_number = None
# Store in the dictionary
br_dict[br_number] = str_number
# Save the dictionary to a CSV file
csv_filename = 'br_str_mapping.csv'
with open(csv_filename, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['BR_Number', 'STR_Number'])
for br_number, str_number in br_dict.items():
writer.writerow([br_number, str_number])
print(f"Dictionary saved to {csv_filename}")Editor is loading...
Leave a Comment