简体   繁体   English

将列表传递给函数python

[英]Passing a list to a function python

def parsePlayer(self,item,response):    #this one works like it should

    item['playerurl'] = re.findall(r'"[^"]*"',"".join(item['playerurl'])) 
    print(item['playerurl'])


def parsePlayer(self,item,response):    #this one does not work like it should

    item['playerurl'] = self.playerurls(item['playerurl'])
    print(item['playerurl'])


def playerurls(*exlist):
    listed = []
    sampString = "".join(exlist)
    listed = re.findall(r'"[^"]*"',sampString)
    return listed

I'm attempting to create a function that will do what the first parsePlayer function does. 我正在尝试创建一个将执行第一个parsePlayer函数的功能的函数。 I'm hoping the playerurls function can take a list and then do a regex function to extract all parts of the string which are in quotations, this is not where I'm having trouble. 我希望playerurls函数可以列出一个列表,然后执行正则表达式函数以提取引号中字符串的所有部分,这并不是我遇到的麻烦。

I am having trouble in writing the playerurls function. 我在编写playerurls函数时遇到麻烦。 I keep getting this: "exceptions.TypeError: sequence item 0: expected string, NbastatsSpider found" @ 我一直得到这个:“ exceptions.TypeError:序列项0:预期的字符串,找到了NbastatsSpider” @

sampString = "".join(exlist)

NbastatsSpider is the name of the spider that fills the 'playerurl' field of the item I created. NbastatsSpider是填充我创建的项目的“ playerurl”字段的蜘蛛的名称。 I don't get why I'm getting this error because I'm passing in a list, turning it into a string, doing the regex function to the string and finally returning a list. 我不明白为什么会收到此错误,因为我要传递一个列表,将其转换为字符串,对字符串执行regex函数,最后返回一个列表。 There must be an error in passing the list (item['playerurl']) to the function. 将列表(item ['playerurl'])传递给函数时必须出错。 Either that or I'm confusing myself with using the 'self.' 要么,要么我使自己困惑于使用“自我”。 parameter. 参数。

It should be: 它应该是:

def playerurls(self, exlist):

You need a self argument because you're calling it as self.playerurls . 您需要一个self变量,因为您将其称为self.playerurls You don't need * before the exlist argument: that's used when the elements of the list are passed as separate arguments. 您不需要在exlist参数前使用* :当列表的元素作为单独的参数传递时使用。

I'm not sure why you're using self in the first place. 我不确定为什么首先要使用self None of these functions refer to any instance variables of self . 这些函数均未引用self任何实例变量。

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

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