import csv import os import re def clean_licence_id(licence_id): # This regex matches the trailing letters (non-digits) and removes them cleaned_licence_id = re.sub(r'\D+$', '', str(licence_id)) # \D+ matches non-digits at the end return cleaned_licence_id def get_player_name_from_csv(licence_id): # Define the file path file_path = '/Users/razmig/Documents/XLR Sport/padelclub_backend/tournaments/static/rankings/CLASSEMENT-PADEL-MESSIEURS-11-2024.csv' cleaned_licence_id = clean_licence_id(licence_id) print(cleaned_licence_id) # Open the file and read it with open(file_path, newline='', encoding='utf-8') as file: reader = csv.reader(file, delimiter=';') # Iterate through each row in the CSV for row in reader: # Ensure the row is not empty and has the expected number of columns if len(row) >= 13: current_licence_id = row[5] # The 5th column contains the licence_id # Check if the current row matches the given licence_id if current_licence_id == str(cleaned_licence_id): # Return first name and last name from the row (3rd and 4th columns) first_name = row[3] # 4th column: first name last_name = row[2] # 3rd column: last name rank = row[1] # 3rd column: last name return first_name, last_name, rank # Return None if no match is found return None, None, None