Python ZeroDivisionError:请求NHL数据并将其写入CSV时被零除

问题描述 投票:0回答:1

我遇到此错误:

Traceback (most recent call last):
  File "C:\Python36\Projects\NHL\HANDICAP\NHL_Indiv_TEAM.py", line 55, in <module>
    item['shotsAgainst']/item['goalsAgainst']+item['shotsAgainst'],
ZeroDivisionError: division by zero

它会一直工作到某一点。我知道我需要除了它才能继续执行整个脚本,但是我在如何以及在哪里写上都在挣扎。我不确定应该在其中写try:还是except行。任何提示都会有很大帮助,谢谢!

import csv
import requests
import os
import datetime

outfile = open("NHL_Indiv_TEAM.csv","w",newline='')
writer = csv.writer(outfile)
header=[
    "TmAbr","Team","Opp","GameLocation","GameId",
    "Date","GP","GF","SF","S%","GA","SA","SV%","W","L","OTL",
    "SoGW","SoGL","PPopp","PPGF","PPPctg","PK",
    "PPGA","PKpctg","TeamPoints","FOW","FOL","FO%","TeamId",

    "BS","GiveA's","TakeA's","HitsFor",

    "Rest",

    "ENgoals","EVgoals","OTgoals","PPgoals","SHgoals",

    "shotAA","shotAAhead","shotAttemptsBehind","shotAttemptsClose","shotAttemptsFor","shotAttemptsTied",
    "unblockedShotAttempts","unblockedShotAttemptsAgainst","unblockedShotAttemptsAhead","unblockedShotAttemptsBehind",
    "unblockedShotAttemptsClose","unblockedShotAttemptsFor","unblockedShotAttemptsTied"]

writer.writerow(header)

req = requests.get('http://www.nhl.com/stats/rest/team?isAggregate=false&reportType=basic&isGame=true&reportName=teamsummary&sort=[{%22property%22:%22teamAbbrev%22,%22direction%22:%22ASC%22},{%22property%22:%22gameDate%22,%22direction%22:%22ASC%22}]&cayenneExp=gameDate%3E=%222016-10-12%22%20and%20gameDate%3C=%222017-04-09%22%20and%20gameTypeId=2') 
data = req.json()['data']

req2 = requests.get('http://www.nhl.com/stats/rest/team?isAggregate=false&reportType=basic&isGame=true&reportName=realtime&sort=[{%22property%22:%22teamAbbrev%22,%22direction%22:%22ASC%22},{%22property%22:%22gameDate%22,%22direction%22:%22ASC%22}]&cayenneExp=gameDate%3E=%222016-10-12%22%20and%20gameDate%3C=%222017-04-09%22%20and%20gameTypeId=2') 
data2 = req2.json()['data']

req3 = requests.get('http://www.nhl.com/stats/rest/team?isAggregate=false&reportType=basic&isGame=true&reportName=teamdaysbetweengames&sort=[{%22property%22:%22teamAbbrev%22,%22direction%22:%22ASC%22},{%22property%22:%22gameDate%22,%22direction%22:%22ASC%22}]&cayenneExp=gameDate%3E=%222016-10-12%22%20and%20gameDate%3C=%222017-04-09%22%20and%20gameTypeId=2') 
data3 = req3.json()['data']

req4 = requests.get('http://www.nhl.com/stats/rest/team?isAggregate=false&reportType=basic&isGame=true&reportName=teamgoalsbytype&sort=[{%22property%22:%22teamAbbrev%22,%22direction%22:%22ASC%22},{%22property%22:%22gameDate%22,%22direction%22:%22ASC%22}]&cayenneExp=gameDate%3E=%222016-10-12%22%20and%20gameDate%3C=%222017-04-09%22%20and%20gameTypeId=2') 
data4 = req4.json()['data']

req5 = requests.get('http://www.nhl.com/stats/rest/team?isAggregate=false&reportType=shooting&isGame=true&reportName=teamsummaryshooting&sort=[{%22property%22:%22teamAbbrev%22,%22direction%22:%22ASC%22},{%22property%22:%22gameDate%22,%22direction%22:%22ASC%22}]&cayenneExp=gameDate%3E=%222016-10-12%22%20and%20gameDate%3C=%222017-04-09%22%20and%20gameTypeId=2')
data5 = req5.json()['data']

# Consider this section of the code and how row is obtained
for item, item2, item3, item4, item5 in zip(data, data2, data3, data4, data5):    
    row = (
        item['teamAbbrev'],
        item['teamFullName'],
        item['opponentTeamAbbrev'],
        item['gameLocationCode'],
        item['gameId'],
        item['gameDate'],
        item['gamesPlayed'],
        item['goalsFor'],
        item['shotsFor'],        
        item['goalsFor']/item['shotsFor'],
        item['goalsAgainst'],
        item['shotsAgainst'],
        # This line is where the traceback occurred:
        item['shotsAgainst']/item['goalsAgainst']+item['shotsAgainst'],
        item['wins'],
        item['losses'],
        item['otLosses'],
        item['shootoutGamesWon'],
        item['shootoutGamesLost'],
        item['ppOpportunities'],
        item['ppGoalsFor'],
        item['ppPctg'],
        item['shNumTimes'],
        item['ppGoalsAgainst'],
        item['penaltyKillPctg'],
        item['points'],
        item['faceoffsWon'],
        item['faceoffsLost'],
        item['faceoffWinPctg'],
        item['teamId'],

        item2['blockedShots'],
        item2['giveaways'],
        item2['takeaways'],
        item2['hits'],

        item3['daysBtwGamesGroup'],

        item4['enGoalsFor'],
        item4['evGoalsFor'],
        item4['otGoals'],
        item4['ppGoalsFor'],
        item4['shGoalsFor'],

        item5['shotAttempts'],
        item5['shotAttemptsAgainst'],
        item5['shotAttemptsAhead'],
        item5['shotAttemptsBehind'],
        item5['shotAttemptsClose'],
        item5['shotAttemptsFor'],
        item5['shotAttemptsTied'],
        item5['unblockedShotAttempts'],
        item5['unblockedShotAttemptsAgainst'],
        item5['unblockedShotAttemptsAhead'],
        item5['unblockedShotAttemptsBehind'],
        item5['unblockedShotAttemptsClose'],
        item5['unblockedShotAttemptsFor'],
        item5['unblockedShotAttemptsTied'],        
        )
    writer.writerow(row)
python csv traceback divide-by-zero
1个回答
2
投票

之所以最终出现除以零的错误,是因为在这种情况下,第55行的item['goalsAgainst']等于item['shotsAgainst']/item['goalsAgainst']等于零。

此外,您还需要注意item['goalsFor']/item['shotsFor'],中的row

由于异常发生在分配给row的一个巨型表达式内,因此请创建一个计算这些比率并捕获ZeroDivisionErrors的函数。如果捕获到ZeroDivisionError,则返回前哨值-1。

计算item['shotsAgainst']/item['goalsAgainst']+item['shotsAgainst'](导致该错误)

def save_ratio(shotsAgainst, goalsAgainst):
    try:
        return shotsAgainst/goalsAgainst + shotsAgainst
    except ZeroDivisionError:
        return -1
...
#replace item['shotsAgainst']/item['goalsAgainst']+item['shotsAgainst'] with
save_ratio(item['shotsAgainst'], item['goalsAgainst']),

计算item['goalsFor']/item['shotsFor'],

def for_ratio(goalsFor, shotsFor):
    try:
        return goalsFor/shotsFor
    except ZeroDivisionError:
        return -1 # Sentinel value, since goalsFor/shotsFor >= 0 otherwise

...
#replace item['goalsFor']/item['shotsFor'] with
for_ratio(item['goalsFor'],item['shotsFor']),
© www.soinside.com 2019 - 2024. All rights reserved.