Skip to main content
 首页 » 编程设计

python之'Int' 对象没有属性

2024年10月01日7jillzhang

我正在尝试创建一个包含职业高尔夫球手数据的列表对象。不同的数据点是高尔夫球手姓名和不同距离的推杆百分比。一旦为每个播放器对象输入了所有数据,我想按名称对这个对象列表进行排序。这些对象的列表称为 PlayerNumber。当我尝试按属性“名称”对 PlayerNumber 进行排序时。我收到一条错误消息,指出“int”没有属性,我不确定为什么 PlayerNumber 被称为整数而不是列表。

如有任何帮助,我们将不胜感激。这是代码:

import operator 
import numpy as np 
import statistics 
import matplotlib.pyplot as plt 
from colour import Color 
from bs4 import BeautifulSoup 
import urllib3 
 
###############    ACCESS WEBPAGES       #################### 
def makeSoup(url): 
    http = urllib3.PoolManager() 
    response = http.request('GET', url) 
    soupdata = BeautifulSoup(response.data) 
    return soupdata 
 
siteURL = [] 
for i in range(7): 
    siteURL.append(i) 
siteURL[0] = '' 
siteURL[1] = 'http://www.pgatour.com/stats/stat.408.html'   #>25 
siteURL[2] = 'http://www.pgatour.com/stats/stat.407.html'   #20-25 
siteURL[3] = 'http://www.pgatour.com/stats/stat.406.html'   #15-20 
siteURL[4] = 'http://www.pgatour.com/stats/stat.405.html'   #10-15 
siteURL[5] = 'http://www.pgatour.com/stats/stat.404.html'   #5-10 
siteURL[6] = 'http://www.pgatour.com/stats/stat.02427.html' #3-5 
 
###############    ACCESS TABLE DATA      ################### 
def row_number(soupdata): 
    for row in table.findAll('tr'): 
        tot_row = row 
    return tot_row 
 
def parse_table(soupdata): 
    currRank = [] 
    prevRank = [] 
    playerName = [] 
    rounds = [] 
    pctMake = [] 
    attempts = [] 
    puttsMade = [] 
    table = soupdata.find('tbody') 
    tot_row = 0 
    for row in table.findAll('tr'): 
        #for col in row.findAll('td'): 
        col = row.find_all('td') 
        #column_1 = col[0] 
        #currRank.append(column_1) 
        #column_2 = col[1] 
        #prevRank.append(column_2) 
        column_3 = col[2].text 
        column_3.strip() 
        playerName.append(column_3) 
        #column_4 = col[3] 
        #rounds.append(column_4)  
        column_5 = col[4].text 
        pctMake.append(column_5)             
        #column_6 = col[5] 
        #attempts.append(column_6)     
        #column_7 = col[6] 
        #puttsMade.append(column_7) 
        tot_row += 1 
    #return currRank, prevRank, playerName, rounds, pctMake, attempts, puttsMade 
    return playerName, pctMake, tot_row 
 
""" 
>25 ft: distance1 
20-25 ft: distance2 
15-20 ft: distance3 
10-15 ft: distance4 
5-10 ft: distance5 
3-5 ft: distance6 
""" 
###############    CLASS DEFINITION      ################### 
class Player: 
    id_list={} 
    def __init__(self,name, id, dis1=0.0, dis2=0.0, dis3=0.0, dis4=0.0, dis5=0.0, dis6=0.0): 
        self.name = name 
        self.dis1 = dis1 
        self.dis2 = dis2 
        self.dis3 = dis3 
        self.dis4 = dis4 
        self.dis5 = dis5 
        self.dis6 = dis6 
        self.id = id 
        Player.id_list[self.name] = self # save the id as key and self as he value 
    def addDis1(self,distance1): 
        self.dis1 = float(distance1) 
    def addDis2(self,distance2): 
        self.dis2 = float(distance2) 
    def addDis3(self,distance3): 
        self.dis3 = float(distance3) 
    def addDis4(self,distance4): 
        self.dis4 = float(distance4) 
    def addDis5(self,distance5): 
        self.dis5 = float(distance5) 
    def addDis6(self,distance6): 
        self.dis6 = float(distance6) 
    def displayPlayer(self): 
        print("Player: ", self.name, '\n' 
              ">25 Ft %: ", self.dis1, '\n' 
              "20-25 Ft %: ", self.dis2, '\n' 
              "15-20 Ft %: ", self.dis3, '\n' 
              "10-15 Ft %: ", self.dis4, '\n' 
              "5-10 Ft %: ", self.dis5, '\n' 
              "3-5 Ft %: ", self.dis6, '\n') 
    @classmethod 
    def lookup_player_name_by_id(cls, name): 
        try: 
            return cls.id_list[name] # return the instance with the id  
        except KeyError: # error check for if id does not exist 
            raise KeyError("No user with id %s" % str(id)) 
 
###############    DATA POPULATION      ################### 
PlayerNumber=[] 
for i in range(0,195): 
    PlayerNumber.append(i) 
for i in range(1,7): 
    soupdata = makeSoup(siteURL[i]) 
    playerName, pctMake, tot_row = parse_table(soupdata) 
    for x in range(0,tot_row): 
        #PlayerNumber.append(x) 
        name = playerName[x] 
        name = name.replace("\xa0", " ") 
        name = name.replace("\n", "") 
        if i == 1: 
            PlayerNumber[x] = Player(name, x) 
            Player.addDis1(PlayerNumber[x],pctMake[x]) 
        if i == 2: 
            val = Player.lookup_player_name_by_id(name) 
            Player.addDis2(PlayerNumber[val.id],pctMake[x]) 
        if i == 3: 
            val = Player.lookup_player_name_by_id(name) 
            Player.addDis3(PlayerNumber[val.id],pctMake[x]) 
        if i == 4: 
            val = Player.lookup_player_name_by_id(name) 
            Player.addDis4(PlayerNumber[val.id],pctMake[x]) 
        if i == 5: 
            val = Player.lookup_player_name_by_id(name) 
            Player.addDis5(PlayerNumber[val.id],pctMake[x]) 
        if i == 6: 
            val = Player.lookup_player_name_by_id(name) 
            Player.addDis6(PlayerNumber[val.id],pctMake[x]) 
 
PlayerNumber.sort(key = operator.attrgetter("name"))          
#PlayerNumber[2].displayPlayer() 

我正在使用 Python 3.4 spyder IDE。作为 FYI,我对 python 比较陌生。

谢谢!

请您参考如下方法:

并不是 PlayerNumber 被称为整数,而是 PlayerNumber 是一个整数列表,并且该列表(和整数)的每个元素都没有属性“名称”,排序( ) 正在尝试访问(以便对它们进行排序)。

编辑:

详细说明,示例中的倒数第二行:

PlayerNumber.sort(key = operator.attrgetter("name"))

正在尝试使用比较函数对 PlayerNumber 进行排序:operator.attrgetter("name"),这意味着它必须对 PlayerNumber 的每个元素调用该函数以获取其在已排序数组中的排名。这就是您尝试从 PlayerNumber 中的整数中获取 .name 属性的原因。