gpt4 book ai didi

python - 类型错误 : split() takes no keyword arguments in Python 2. x

转载 作者:太空狗 更新时间:2023-10-29 17:56:14 36 4
gpt4 key购买 nike

我试图将文档的一部分分成不同的部分,这些部分由&符号分隔。这是我的:

name,function,range,w,h,k,frac,constraint = str.split(str="&", num=8)

错误:

TypeError: split() takes no keyword arguments

有人可以向我解释错误并提供替代方法让我完成这项工作吗?

最佳答案

str.split的参数分别称为 sepmaxsplit:

str.split(sep="&", maxsplit=8)

但是你只能在 Python 3.x 中使用这样的参数名称。在 Python 2.x 中,您需要执行以下操作:

str.split("&", 8)

在我看来,这对两个版本都是最好的,因为使用名称实际上只是多余的。 str.split 是 Python 中非常有名的工具,所以我怀疑任何 Python 程序员都难以理解该方法的参数的含义。

此外,您应该避免使用户定义的名称与内置名称之一相同。这样做会使内置函数黯然失色,并使其在当前范围内无法使用。因此,除了 str 之外,我会为您的字符串选择一个不同的名称。

关于python - 类型错误 : split() takes no keyword arguments in Python 2. x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27493703/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com