类型错误:split()不接受关键字参数

时间:2023-02-07 21:45:18

I am trying to separate a section of a document into its different components which are separated by ampersands. This is what I have:

我试图将文档的一个部分分离到由&号分隔的不同组件中。这就是我所拥有的:

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

Error:

错误:

TypeError: split() takes no keyword arguments

Can someone explain the error to me and also provide an alternate method for me to make this work?

谁能给我解释一下这个错误,并为我提供另一种方法来实现这个功能吗?

3 个解决方案

#1


17  

The parameters of str.split are called sep and maxsplit:

string .split的参数分别为sep和maxsplit:

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

But you can only use the parameter names like this in Python 3.x. In Python 2.x, you need to do:

但是您只能在Python 3.x中使用这样的参数名。在Python中2。x,你需要做:

str.split("&", 8)

which in my opinion is the best for both versions since using the names is really just redundant. str.split is a very well known tool in Python, so I doubt any Python programmers will have trouble understanding what the arguments to the method mean.

在我看来,这对两个版本都是最好的,因为使用名字是多余的。split是Python中一个非常有名的工具,因此我怀疑任何Python程序员都不会理解方法的参数的含义。

Also, you should avoid making user-defined names the same as one of the built-in names. Doing this overshadows the built-in and makes it unusable in the current scope. So, I'd pick a different name for your string besides str.

此外,应该避免让用户定义的名称与内置名称中的一个名称相同。这样做会使内置程序蒙上阴影,使其在当前范围内无法使用。那么,除了str,我还要为你的字符串取一个不同的名字。

#2


1  

The error states that you can't provide named arguments to split. You have to call split with just the arguments - without the names of the arguments:

错误声明不能为split提供命名参数。你必须只使用参数调用split——不使用参数的名称:

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

#3


1  

split doesnt get keyword arguments str or num. Do this instead:

split不会得到关键字参数str或num。

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

#1


17  

The parameters of str.split are called sep and maxsplit:

string .split的参数分别为sep和maxsplit:

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

But you can only use the parameter names like this in Python 3.x. In Python 2.x, you need to do:

但是您只能在Python 3.x中使用这样的参数名。在Python中2。x,你需要做:

str.split("&", 8)

which in my opinion is the best for both versions since using the names is really just redundant. str.split is a very well known tool in Python, so I doubt any Python programmers will have trouble understanding what the arguments to the method mean.

在我看来,这对两个版本都是最好的,因为使用名字是多余的。split是Python中一个非常有名的工具,因此我怀疑任何Python程序员都不会理解方法的参数的含义。

Also, you should avoid making user-defined names the same as one of the built-in names. Doing this overshadows the built-in and makes it unusable in the current scope. So, I'd pick a different name for your string besides str.

此外,应该避免让用户定义的名称与内置名称中的一个名称相同。这样做会使内置程序蒙上阴影,使其在当前范围内无法使用。那么,除了str,我还要为你的字符串取一个不同的名字。

#2


1  

The error states that you can't provide named arguments to split. You have to call split with just the arguments - without the names of the arguments:

错误声明不能为split提供命名参数。你必须只使用参数调用split——不使用参数的名称:

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

#3


1  

split doesnt get keyword arguments str or num. Do this instead:

split不会得到关键字参数str或num。

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