txt2csv.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama 2 Community License Agreement.
  3. import csv
  4. # Define the input and output file names
  5. input_file = 'nba.txt'
  6. output_file = 'nba_roster.csv'
  7. # Initialize lists to store data
  8. roster_data = []
  9. current_team = None
  10. # Open the input file
  11. with open(input_file, 'r') as file:
  12. for line in file:
  13. # Remove leading and trailing whitespaces from the line
  14. line = line.strip()
  15. # Check if the line starts with 'https', skip it
  16. if line.startswith('https'):
  17. continue
  18. # Check if the line contains the team name
  19. if 'Roster' in line:
  20. current_team = line.split(' Roster ')[0]
  21. elif line and "NAME" not in line: # Skip empty lines and header lines
  22. # Split the line using tabs as the delimiter
  23. player_info = line.split('\t')
  24. # Remove any numbers from the player's name and set Jersey accordingly
  25. name = ''.join([c for c in player_info[0] if not c.isdigit()])
  26. jersey = ''.join([c for c in player_info[0] if c.isdigit()])
  27. # If no number found, set Jersey to "NA"
  28. if not jersey:
  29. jersey = "NA"
  30. # Append the team name, name, and jersey to the player's data
  31. player_info = [current_team, name, jersey] + player_info[1:]
  32. # Append the player's data to the roster_data list
  33. roster_data.append(player_info)
  34. # Write the data to a CSV file
  35. with open(output_file, 'w', newline='') as csvfile:
  36. writer = csv.writer(csvfile)
  37. # Write the header row
  38. writer.writerow(['Team', 'NAME', 'Jersey', 'POS', 'AGE', 'HT', 'WT', 'COLLEGE', 'SALARY'])
  39. # Write the player data
  40. writer.writerows(roster_data)
  41. print(f'Conversion completed. Data saved to {output_file}')