簡體   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