With the following piece of code, when a keyword is an empty string the findall method searches the content for spaces. How can I get findall to return 0 when given an empty string?
def match():
row_list = [[1711,"How to find a new fitness fitness fitness fitness gym that will work for you","<p>In general, I like fitness and new things.</p>","https://www.vida.com/content/how-to-find-a-gym-that-will-work-for-you","","new","gym"],[1711,"How to find a gym that will work for you","<p>Finding a gym is a fun and an exciting part of your health journey. When looking for a gym consider what is important to you. There are many gyms out there from the bare bones gym to the full service athletic club. For example you may want access to a pool and jacuzzi, classes that are included in your membership, specific exercise equipment or child care services.</p>","https://www.vida.com/content/how-to-find-a-gym-that-will-work-for-you","General fitness","new","gym"]]
keywords_per_article = []
for row in row_list:
content = [row[1],row[2],row[3]]
keywords = [row[4],row[5],row[6]]
result_list = []
for keyword in keywords:
print keyword
keyword_list = []
for content_item in content:
print content_item
keywords_found = re.findall(keyword, content_item)
keyword_number = len(keywords_found)
keyword_list.append(keyword_number)
keyword_total = sum(keyword_list)
result_list.append(keyword_total)
keywords_per_article.append(result_list)
print keywords_per_article
The result:
[[197, 2, 2], [0, 0, 6]]
ifcondition.