Python ZeroDivisionError: division by zero occurs while requesting NHL data and writing it into a CSV

Python ZeroDivisionError: division by zero occurs while requesting NHL data and writing it into a CSV

我 运行 遇到了这个错误:

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)

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

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

由于异常发生在分配给 row 的一个巨型表达式中,因此创建一个函数来计算这些比率,并捕获 ZeroDivisionErrors。如果捕获到 ZeroDivisionError,return 标记值 -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']),