微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

读取csv文件时重置循环的问题

如何解决读取csv文件时重置循环的问题

在我的NBA模拟代码中,该代码允许用户从相同或不同的季节中选择2支球队。如果用户从2019-20赛季中选择了两支球队,则将填充第1队和第1队的对手阵,但第2队和第2队的战队将为空。季节和团队的其他所有组合都可以发挥作用,例如2019-20赛季第1队和2015-16赛季第2队。我很确定问题是与if语句中的for循环有关的,以检查season2是否等于2019-20。

import csv
from nba_api.stats.static import teams
import random as rnd
import numpy as np

team1_search = teams.find_teams_by_full_name('Bucks')
team1_abbr = team1_search[0]['abbreviation']

team2_search = teams.find_teams_by_full_name('Lakers')
team2_abbr = team2_search[0]['abbreviation']

with open('nba_stats/stats/data/1949-2018_officialBoxscore.csv','r') as csv_file:
        csv_dict_reader = csv.DictReader(csv_file)

        seasons = []
        for row in csv_dict_reader:
            seasons.append(row['season'])

        set_res = set(seasons)
        seasons = (list(set_res))

season1 = '2019-20'
season2 = '2019-20'

if season1 == '2019-20' or season2 == '2019-20':
    with open('nba_stats/stats/data/2019-20_officialBoxscore.csv','r') as csv_file:
        csv_dict_reader = csv.DictReader(csv_file)

        if season1 == '2019-20':
            team1PTS = []
            oppt1PTS = []
            for row in csv_dict_reader:         
                if row['teamAbbr'] == team1_abbr and row['seasonType'] == 'Regular':
                    team1PTS.append(float(row['teamPTS']))
                    oppt1PTS.append(float(row['opptPTS']))
                        
                elif row['opptAbbr'] == team1_abbr and row['seasonType'] == 'Regular':
                    team1PTS.append(float(row['opptPTS']))
                    oppt1PTS.append(float(row['teamPTS']))

        if season2 == '2019-20':
            team2PTS = []
            oppt2PTS = []
            #I'm pretty sure this for loop is the issue after a couple of tests
            for row in csv_dict_reader:   
                if row['teamAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['teamPTS']))
                    oppt2PTS.append(float(row['opptPTS']))

                elif row['opptAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['opptPTS']))
                    oppt2PTS.append(float(row['teamPTS']))

期望的行为

Bucks: 98
Lakers: 117

错误

Warning: Mean of empty slice.
  return _methods._mean(a,axis=axis,dtype=dtype,C:\Users\panos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\python38\site-packages\numpy\core\_methods.py:161: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
C:\Users\panos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\python38\site-packages\numpy\core\_methods.py:216: RuntimeWarning: degrees of freedom <= 0 for slice
  ret = _var(a,out=out,ddof=ddof,C:\Users\panos\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\python38\site-packages\numpy\core\_methods.py:185: RuntimeWarning: invalid value encountered in true_divide
LocalCache\local-packages\python38\site-packages\numpy\core\_methods.py:209: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
e 106,in <module>
    team1_sim_pts = int(round((rnd.gauss(team1_mean,team1_std) + rnd.gauss(opp2_mean,opp2_std))) / 2)

解决方法

在第一个for循环之后,文件中的所有内容都已被读取,因此,第二个循环将不再需要读取任何内容。开始新的for循环不会将文件重新设置为开始。您需要先回到文件的开头,然后再尝试读取它。

        if season2 == '2019-20':
            team2PTS = []
            oppt2PTS = []
            csv_file.seek(0) # rewind to beginning of file
            for row in csv_dict_reader:   
                if row['teamAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['teamPTS']))
                    oppt2PTS.append(float(row['opptPTS']))

                elif row['opptAbbr'] == team2_abbr and row['seasonType'] == 'Regular':
                    team2PTS.append(float(row['opptPTS']))
                    oppt2PTS.append(float(row['teamPTS']))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。