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?

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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)]

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.