我正试图从python NBA\u API中提取以前的NBA游戏,但是我的格式没有按我需要的那样工作。我希望数据流中的每一行都被分离到列中的每一行
import nba_api
import requests
import csv
from nba_api.stats.static import teams
from nba_api.stats.endpoints import leaguegamefinder
from nba_api.stats.endpoints import leaguegamelog
season = leaguegamelog.LeagueGameLog("00")
nba_teams = teams.get_teams()
seasonGames = season.get_data_frames()[0]
seasonGames.head()
print(seasonGames.TEAM_NAME)
with open('NBAStats.csv', mode='w') as csv_file:
fieldnames = ['team_name','team_id','game_id']
writer = csv.DictWriter(csv_file, fieldnames=fieldnames,delimiter=',')
writer.writeheader()
for TEAM_NAME in seasonGames['TEAM_NAME']:
writer.writerow({'team_name': seasonGames["TEAM_NAME"],'team_id': seasonGames["TEAM_ID"],'game_id': seasonGames["GAME_ID"]})
print("done")
这就是输出的样子:https://imgur.com/a/YdqoR6p
这就是你想要的吗?你知道吗
或者使用更简单的方法:
相关问题 更多 >
编程相关推荐