简体   繁体   English

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

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

What am I missing here?我在这里缺少什么? I have tried looking at the code but I do not know where the extra positional argument is located at,我曾尝试查看代码,但我不知道额外的位置参数位于何处,

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

Your error is elsewhere, not in the snippet provided, since as a standalone function, that code should work fine.您的错误在其他地方,而不是在提供的代码段中,因为作为独立函数,该代码应该可以正常工作。

Looking at your traceback, you've introduced classes, so reading your error看看你的回溯,你已经引入了类,所以阅读你的错误

2 were given 2 被给予

When you call a class function, the instance of that class is always supplied as a parameter当您调用类函数时,该类的实例始终作为参数提供

example例子

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

So, to fix your function, you need to add the self parameter, even if you don't plan on using it.因此,要修复您的函数,您需要添加 self 参数,即使您不打算使用它。

Before, the path variable you used wasn't actually a string, so the type checker should have been throwing an error as well之前,您使用的path变量实际上不是字符串,因此类型检查器也应该抛出错误

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

暂无
暂无

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

相关问题 TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given - TypeError: __init__() takes from 1 to 3 positional arguments but 4 were given Money 和 TypeError:__init__() 需要 1 到 2 个位置参数,但给出了 3 个 - Money and TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given 类型错误:execute() 需要 2 到 3 个位置参数,但给出了 7 个 - TypeError: execute() takes from 2 to 3 positional arguments but 7 were given TypeError:send()接受1到2个位置参数,但给出了3个 - TypeError: send() takes from 1 to 2 positional arguments but 3 were given Python/MySQL TypeError:execute() 需要 2 到 4 个位置参数,但给出了 5 个 - Python/MySQL TypeError: execute() takes from 2 to 4 positional arguments but 5 were given 类型错误:raw_input() 需要 1 到 2 个位置参数,但给出了 4 个 - TypeError: raw_input() takes from 1 to 2 positional arguments but 4 were given Django TypeError:url()需要2到4个位置参数,但是给出了16个 - Django TypeError: url() takes from 2 to 4 positional arguments but 16 were given 继承 TypeError: __init__() 接受 1 到 2 个位置参数,但给出了 8 个 - inheritance TypeError: __init__() takes from 1 to 2 positional arguments but 8 were given TypeError: append() 从 2 到 5 个位置 arguments 但给出了 8 个 - TypeError: append() takes from 2 to 5 positional arguments but 8 were given TypeError: with_column() 从 3 到 4 个位置 arguments 但给出了 5 个(python) - TypeError: with_column() takes from 3 to 4 positional arguments but 5 were given (python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM