請(qǐng)回答我,我已經(jīng)編碼了10天了。我在執(zhí)行代碼中的循環(huán)時(shí)遇到麻煩,但是我可以肯定的是,這是因?yàn)槲业玫搅嘶厮?。我使用以下代碼解析從url獲得的xml文件:pattern4 = re.compile('title=\'Naps posted: (.*) Winners:')pattern5 = re.compile('Winners: (.*)\'><img src=')for row in xmlload1['rows']: cell = row["cell"]##### defining the Keys (key is the area from which data is pulled in the XML) for use in the pattern finding/regex user_delimiter = cell['username'] selection_delimiter = cell['race_horse']##### the use of the float here is to make sure the result of the strike rate calculations returns as a decimal, otherwise python 2 rounds to the nearest integer! user_numberofselections = float(re.findall(pattern4, user_delimiter)[0]) user_numberofwinners = float(re.findall(pattern5, user_delimiter)[0]) strikeratecalc1 = user_numberofwinners/user_numberofselections strikeratecalc2 = strikeratecalc1*100##### Printing the results of the code at hand print "number of selections = ",user_numberofselections print "number of winners = ",user_numberofwinners print "Strike rate = ",strikeratecalc2,"%" print ""getData()此代碼與其余代碼一起返回:number of selections = 112.0number of winners = 21.0Strike rate = 18.75 %number of selections = 146.0number of winners = 21.0Strike rate = 14.3835616438 %number of selections = 163.0number of winners = 55.0Strike rate = 33.7423312883 %現(xiàn)在,此xmlload的結(jié)果表明,只有三個(gè)用戶要解析,但是有第4個(gè)whos數(shù)據(jù)會(huì)讀取number of selections = 0number of winners = 0Strike rate = 0出于我的目的,對(duì)于沒(méi)有跟蹤記錄的用戶,沒(méi)有必要拉入用戶統(tǒng)計(jì)信息,我該如何使代碼跳過(guò)選擇0的用戶,或者至少使其不被零除的錯(cuò)誤不會(huì)影響要在循環(huán)中運(yùn)行的代碼?
對(duì)已解析的xml數(shù)據(jù)執(zhí)行計(jì)算時(shí),如何避免零錯(cuò)誤除法
達(dá)令說(shuō)
2021-03-16 09:13:59