2015-01-11 2 views
0

С помощью следующей части кода, когда ключевое слово является пустой строкой, метод findall ищет содержимое для пробелов. Как я могу заставить findall возвращать 0 при заданной пустой строке?Получение re.findall() для игнорирования пустых строк в python?

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 

Результат:

[[197, 2, 2], [0, 0, 6]] 
+4

Окружите его с простым 'if' состоянии. – Maroun

ответ

1
   if keyword: 
        keywords_found = re.findall(keyword, content_item) 
        keyword_number = len(keywords_found) 
       else: 
        keyword_number = 0 
Смежные вопросы