txt2csv.py 1.8 KB

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