maximum recursion error Python
我正试图从espn.com上搜集一些盒子分数,并将它们放入熊猫数据框中。我过去也做过类似的事情,没有任何问题。但是,在这种情况下,当我尝试保存数据帧时,会收到这个错误。
RuntimeError:调用python对象时超过了最大递归深度
当我试图将其保存为HDF5表时,也会遇到类似的错误。
即使是这段代码也会产生相同的错误。我很困惑为什么要这样做?这和功能有关吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | url = 'http://espn.go.com/nba/boxscore?gameId=400278773' boxurl = urllib2.urlopen(url).read() soup = BeautifulSoup(boxurl) tables = soup.findAll('table') lineScoreTable = tables[-2] lineScoreRows = lineScoreTable.findAll('tr') def GetAwayQTRScores(): scoreRow = lineScoreRows[1].findAll('td') awayQTRScores = [] for x in scoreRow: scores = x.string awayQTRScores.append(scores) return awayQTRScores # returns list awayQTRScores = GetAwayQTRScores() awayTeam = awayQTRScores[0] awayQ1 = int(awayQTRScores[1]) awayQ2 = int(awayQTRScores[2]) awayQ3 = int(awayQTRScores[3]) awayQ4 = int(awayQTRScores[4]) awayOT1 = np.nan if len(awayQTRScores) < 7 else int(awayQTRScores[5]) awayOT2 = np.nan if len(awayQTRScores) < 8 else int(awayQTRScores[6]) awayOT3 = np.nan if len(awayQTRScores) < 9 else int(awayQTRScores[7]) awayOT4 = np.nan if len(awayQTRScores) < 10 else int(awayQTRScores[8]) data = {'AwayTeam' :[awayTeam], 'AwayQ1' : [awayQ1], 'AwayQ2' : [awayQ2], 'AwayQ3' : [awayQ3], 'AwayQ4' : [awayQ4], 'AwayOT1' : [awayOT1], 'AwayOT2' : [awayOT2], 'AwayOT3' : [awayOT3], 'AwayOT4' : [awayOT4]} testScrape = pd.DataFrame(data) testScrape.save('testScrape') |
运行时错误跟踪(最近调用的最后一个)在()---->1 testscrape.save('testscrape')
保存中的c:python27libsite packagespandascoregeneric.pyc(self,path)二十六27 def save(self,路径):--->28 com.save(自我,路径)二十九30@分类法
保存中的c:python27libsite packagespandascorecommon.pyc(obj,path)1562 F=打开(路径‘wb’)1563尝试:->1564 pickle.dump(obj,f,protocol=pickle.highest_协议)1565最后:1566 F.关闭()
RuntimeError:调用python对象时超过了最大递归深度
1 | print data |
收益率
'awayteam':[u'lal'],'awayot4':[nan],'awayq4':[27],'awayq3':[36],'awayq2':[24],'awayq1':[16],'awayot1':[nan],'awayot2':[nan],'awayot3':[nan]
pickle.dump中的这个异常通常意味着您试图pickle包含其自身的对象(直接或间接)。
但是什么对象包含它自己呢?当你把它们都拿出来的时候,它们看起来都很好。
这是
1 | awayTeam = awayQTRScores[0] |
您可能不会从
但它也没有定义自定义的pickler,所以它使用默认的pickler。一般来说,
If you want to use a
NavigableString outside of Beautiful Soup, you should callunicode() on it to turn it into a normal Python Unicode string. If you don’t, your string will carry around a reference to the entire Beautiful Soup parse tree, even when you’re done using Beautiful Soup.
当然,解析树包含一个对字符串的引用,等等,所以这个类型永远不能被pickle。
解决方案很简单。你想要的是一个普通的旧的
1 | awayTeam = unicode(awayQTRScores[0]) |