TypeError: read_csv()接受0到1个位置参数,但给出了2个



我在这里错过了什么?我试着看代码,但我不知道额外的位置参数位于哪里,

def read_csv(path: str = None) -> List[List] :
lead = Path(__file__).parent / f'../data/{path}'
entries = []
print('Reading dataset...')
with open(lead, 'r') as csvfile:
video_reader = csv.reader(csvfile)
for row in video_reader:
entries.append(row)
return print(entries)
Traceback (most recent call last):
File "C:/Users/MEDIAMARKT/Desktop/booking/__main__.py", line 15, in <module>
gui = GUI()
File "C:UsersMEDIAMARKTDesktopbookinggui.py", line 22, in init
self.upload_data()
File "C:UsersMEDIAMARKTDesktopbookinggui.py", line 84, in upload_data
self.booking.read_csv(path)
TypeError: read_csv() takes from 0 to 1 positional arguments but 2 were given

您的错误在其他地方,而不是在提供的代码片段中,因为作为一个独立的函数,该代码应该可以正常工作。

查看您的回溯,您已经引入了类,因此读取错误

2例

调用类函数时,该类的实例总是作为参数

提供

例子
class Foo:
def bar(self, other=None):  # self is a required argument for instance methods
pass
xyz = 'something'
booking = Foo()
booking.bar()  # 1 parameter provided - the instance itself
booking.bar(xyz)  # 2 parameters provided - the instance and the string

所以,为了修复你的函数,你需要添加self参数,即使你不打算使用它。

之前,您使用的path变量实际上不是字符串,因此类型检查器也应该抛出错误

class Booking():
def read_csv(self, path:str = None) -> List[List]:
# TODO
return []