0

I'm working with scrapy 1.1 . After extracting entities I get a string that looks like:

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

I want to get a list of all the companies, so I tried:

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]

I'm getting:

company_list= [a for a in entity_list if any(company_types) in a]
TypeError: 'in <string>' requires string as left operand, not bool

What am I doing wrong?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • Answered in http://stackoverflow.com/questions/4843158/check-if-a-python-list-item-contains-a-string-inside-another-string – gregory Mar 09 '17 at 03:57

3 Answers3

2

The problem here:

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

Remember that any() is just a function which has to return a single value... and the code would check to see if that single value is in a, which is not what you want.

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

Basically, the parentheses were in the wrong place. Kind of.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
2

I think you are looking for the information on this page: How to check if a string contains an element from a list in Python

try changing:

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

to

company_list = [a in entity_list if any(company in a for company in company_types)]
Community
  • 1
  • 1
Vince W.
  • 3,561
  • 3
  • 31
  • 59
1

You can't use any and all like that. Those functions return whether if any or all of what you passed them is true, so they return True or False, not some magic thing you can use with in.