我遇到了这个错误:
Traceback (most recent call last):
File "C:\Users\#####\Python\Second Shot - Classes.py", line 159, in <module>
d = Demonstrator("####.csv")
File "C:\Users\####\Python\Second Shot - Classes.py", line 142, in __init__
self.reader.run()
File "C:\Users\####\Python\Second Shot - Classes.py", line 97, in run
self.counter.add( Rectangle(theme, channels, owing, status, mark, code))
TypeError: __init__() missing 5 required positional arguments: 'channels', 'owing', 'status', 'mark', and 'code'
我理解它通常是因为我打电话给一个班而没有给它一个参数但我在这里没有看到它?我不明白我需要在错误中给出位置参数。代码需要一个.csv,我使用函数add中的信息给我一个结果。参数是.csv im中的列。
from rect import Rectangle
class Counter():
def __init__(self):
self.count = 0
self.area = 0
self.Top3 = 0
self.finalVal = 0
self.TotalOwing = 0
self.Columnbounter= {}
def add(self, rectangle):
if rectangle.getTheme() in self.Columnbounter:
self.Columnbounter[rectangle.getTheme()]+=1
else:
self.Columnbounter[rectangle.getTheme()]=1
self.popularDict= sorted(self.Columnbounter, key = self.Columnbounter.get, reverse = True)
self.Top3 = self.popularDict[:1]
def question1(self):
return ("Question 1, Most Common String: %d " %(self.Top3))
def reset(self):
self.count = 0
self.area = 0
self.Top3 = 0
self.finalVal = 0
self.TotalOwing = 0
self.Columnbounter= {}
##-------------------
## FileReader class
##--------------------
class FileReader():
def __init__(self, filename, counter):
self.counter = counter
self.filename = filename
def run(self):
theFile = open( self.filename,'r')
firstLine = True
for line in theFile:
if firstLine:
firstLine = False
continue
line=line.strip()
fields=line.split(",")
theme= str( fields[0])
channels= int( fields[1])
owing= float( fields[2])
status= str( fields[3])
mark= int( fields[4])
code= fields[5]
self.counter.add( Rectangle(theme, channels, owing, status, mark, code))
theFile.close()
##--------------------
## Demonstrator class
##
##--------------------
class Demonstrator():
def __init__(self,filename):
self.counter = Counter()
self.reader = FileReader(filename, self.counter)
self.reader.run()
def Q1Answer(self):
print(self.counter.question1())
def newFile(self, filename):
self.counter.reset() # throw away any old results
self.reader = FileReader( filename, self.counter)
self.reader.run()
#----------------------------
#----------------------------
print("\n\t My Work","+"*30)
#print("\nFirst calculating with the file reca.csv")
d = Demonstrator("######.csv")
d.Q1Answer()
这是导入的模块,如果有帮助:
class Rectangle():
def __init__(self, theme, channels, owing, status, mark, code):
self.theme= theme
self.channels= channels
self.owing= owing
self.status= status
self.mark= mark
self.code= code
def getTheme(self):return self.theme
def getChannels(self):return self.channels
def getOwing(self):return self.owing
def getStatus(self):return self.status
def getMark(self):return self.mark
def getCode(self):return self.code