繁体   English   中英

类型错误:read_csv() 需要 0 到 1 个位置参数,但给出了 2 个

[英]TypeError: read_csv() takes from 0 to 1 positional arguments but 2 were given

我在这里缺少什么? 我曾尝试查看代码,但我不知道额外的位置参数位于何处,

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:\Users\MEDIAMARKT\Desktop\booking\gui.py", line 22, in init
          self.upload_data()
        File "C:\Users\MEDIAMARKT\Desktop\booking\gui.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 []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM