Python ZeroDivisionError:除以零

时间:2018-05-03 22:09:47

标签: python csv traceback divide-by-zero

我遇到了这个错误:

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)

1 个答案:

答案 0 :(得分:2)

您最终遇到“零除零”错误的原因是,在这种情况下,第55行中的item['goalsAgainst']item['shotsAgainst']/item['goalsAgainst']等于零。

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

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