TypeError:'in <string>'需要字符串作为左操作数,而不是bool

时间:2017-03-09 02:53:05

标签: python

我正在使用scrapy 1.1。提取实体后,我得到一个看起来像这样的字符串:

select case when charindex('-',substring(@test, charindex('SRID=', @test)+5, 8000))-1 < 0
        then substring(@test, charindex('SRID=', @test)+5, 8000)
        else iif(charindex('SRID=', @test) > 0, substring(@test, charindex('SRID=', @test)+5, charindex('-',substring(@test, charindex('SRID=', @test)+5, 8000))-1),'')
        end

我想获得所有公司的清单,所以我尝试了:

bob jones | acme, inc | jeff roberts |company, llc

我得到了:

company_types = ['inc','llc','corp']
entities = str(item.get('entities')).lower
entity_list = entities.split('|')
company_list= [a in entity_list if any(company_types) in a]

我做错了什么?

3 个答案:

答案 0 :(得分:2)

这里的问题:

company_list = [a for a in entity_list if any(company_types) in a]

请记住,any()只是一个必须返回单个值的函数...代码会检查该单个值是否在a中,这不是您想要的。

company_list = [a for a in entity_list if any(t in a for t in company_types)]

基本上,括号在错误的地方。那种。

答案 1 :(得分:2)

我认为您正在寻找此页面上的信息: How to check if a string contains an element from a list in Python

尝试改变:

company_list= [a in entity_list if any(company_types) in a]

company_list = [a in entity_list if any(company in a for company in company_types)]

答案 2 :(得分:1)

您不能像这样使用anyall。这些函数会返回您传递的内容是否为真,因此返回TrueFalse,而不是in可以使用的神奇内容。

相关问题