1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import csv
- input_file = 'nba.txt'
- output_file = 'nba_roster.csv'
- roster_data = []
- current_team = None
- with open(input_file, 'r') as file:
- for line in file:
-
- line = line.strip()
-
-
- if line.startswith('https'):
- continue
-
-
- if 'Roster' in line:
- current_team = line.split(' Roster ')[0]
- elif line and "NAME" not in line:
-
- player_info = line.split('\t')
-
-
- name = ''.join([c for c in player_info[0] if not c.isdigit()])
- jersey = ''.join([c for c in player_info[0] if c.isdigit()])
-
-
- if not jersey:
- jersey = "NA"
-
-
- player_info = [current_team, name, jersey] + player_info[1:]
-
-
- roster_data.append(player_info)
- with open(output_file, 'w', newline='') as csvfile:
- writer = csv.writer(csvfile)
-
-
- writer.writerow(['Team', 'NAME', 'Jersey', 'POS', 'AGE', 'HT', 'WT', 'COLLEGE', 'SALARY'])
-
-
- writer.writerows(roster_data)
- print(f'Conversion completed. Data saved to {output_file}')
|