如何解决我收到一个``AttributeError:'int'对象没有属性'cityArray'`''
这是我代码的一部分,无论运行此代码时是否出现属性错误,我都打印了一些值,而不是仅仅出于检查目的而返回它们。我确实定义了cityArray,但是它仍然引发错误。
class City:#City classs
def __init__(self,cid,cname,cstate,pop,cities): #initilize the variables
self.cid= cid
self.cname= cname ##sets them all as empty lists
self.cstate=cstate
self.pop= pop
self.cities= cities
def __str__(self): #return fuction to put out the final value
print("cid: %s; cname: %s;cstate: %s; cases:%s"%(cid,cities))
class COV19Library:#library classs
def __init__(self):
self.issorted=False
self.cityArray=[]
self.size=0
def LoadData(self,filename: str): #loads the data feom excel sheet into those empty lists
file = csv.DictReader(open(filename)) #as it is in a directory everything is linked
for temp in file:
cid = temp["City_ID"] #all the city ID
pop = temp['POP10'] #the population
cities = temp['6/30/20'] #the latest number of case
t = temp['City State'].split() #splits city and state
cstate = [word for word in t if word.isupper()] #A comprehension and a Bool to identify states
cname = [x for x in t if x not in cstate] #remove the states from the initial list and we get cities
#print(cname) #test
for r in range(len(cid)):
self.cityArray.append(City(cid,cities))
def linearSearch(self,city,attribute): #linear search algorythum
#print(cid)
if attribute == "name":
for r in cname:
if r == city:
print(r)
else:
print("City not found")
elif attribute=="id":
for z in self.cid:
if z == city:
print(z)
else:
print("City not found")```
ERROR:
Traceback (most recent call last):
File "Proj.py",line 142,in <module>
COV19Library.LoadData(0,'cov19_city.csv')
File "Proj.py",line 36,in LoadData
self.cityArray.append(City(cid,cities))
AttributeError: 'int' object has no attribute 'cityArray'
解决方法
错误:
COV19Library.LoadData(0,'cov19_city.csv')
该调用将0作为第一个参数传递。此数字已分配给self
,这会导致错误。
创建该类的实例并使用它:
covid = COV19Library()
covid.LoadData('a_file_name')
,
从错误回溯来看,似乎有错误的行是:
COV19Library.LoadData(0,'cov19_city.csv')
从您的类中,我们可以看到LoadData接受了自身的一个实例以及csv文件的文件名。您将第一个参数传递为0,这将导致LoadData加载实例0,这将创建该int错误。
要解决此问题,您需要创建类的实例,例如:
library = COV19Library()
library.LoadData('cov19_city.csv')
我也可以问一下,为什么在同一函数中循环两次?如果放了,会不会更容易
self.cityArray.append(City(cid,cname,cstate,pop,cities))
紧接着
cname = [x for x in t if x not in cstate]
编辑:
class City: # City classs
def __init__(self,cid,cities): # initilize the variables
self.cid = cid
self.cname = cname # sets them all as empty lists
self.cstate = cstate
self.pop = pop
self.cities = cities
def __str__(self): # return fuction to put out the final value
print("cid: %s; cname: %s;cstate: %s; cases:%s" %
(cid,cities))
class COV19Library: # library classs
def __init__(self):
self.issorted = False
self.cityArray = []
self.size = 0
def LoadData(self,filename: str): # loads the data feom excel sheet into those empty lists
# as it is in a directory everything is linked
file = csv.DictReader(open(filename))
for temp in file:
cid = temp["City_ID"] # all the city ID
pop = temp['POP10'] # the population
cities = temp['6/30/20'] # the latest number of case
t = temp['City State'].split() # splits city and state
# A comprehension and a Bool to identify states
cstate = [word for word in t if word.isupper()]
# remove the states from the initial list and we get cities
cname = [x for x in t if x not in cstate]
# print(cname) #test
self.cityArray.append(City(cid,cities))
def linearSearch(self,city,attribute): # linear search algorythum
# print(cid)
for r in self.cityArray:
if r.attribute == city:
print(r)
else:
print("City not found")
要回答您的评论,请不断获取AttributeError:'COV19Library'对象没有属性'cname'
如果您查看COV19Library类的构造函数:
class COV19Library: # library classs
def __init__(self):
self.issorted = False
self.cityArray = []
self.size = 0
cname不是此方法中的变量之一,因此它在库中不存在。不过,如果您调用City类的实例(这是cityArray存储的内容),则可以访问Cname。
知道这一点后,您可以遍历城市数组:
def linearSearch(self,attribute): # linear search algorythum
# print(cid)
for r in self.cityArray:
if r.attribute == city:
print(r)
else:
print("City not found")
要调用此方法,您可以执行以下操作:
library.linearSearch('TX',cstate)
让我们完成每个步骤:
- 调用该函数时,它将接收COV19Library的当前实例,您要查找的值(字符串或整数)以及要传递的属性。(变量名)
- 程序循环遍历您在LoadData()中已经创建的city数组,每个循环变量称为r
- 它比较r的属性,并查看您传入的值是否与r的属性值相同
- 这会一直循环直到找到它,或者它不存在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。