让re.findall()忽略python中的空字符串?

时间:2015-01-11 08:56:30

标签: python regex

使用以下代码,当关键字为空字符串时,findall方法会在内容中搜索空格。如果给出一个空字符串,怎么能让findall返回0?

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

1 个答案:

答案 0 :(得分:1)

var JSQmessages = [ModelMessageBridge]()
//fill up the array here

self.collectionView.performBatchUpdates({

   // indexPaths for earlier messages
   let lastIdx = history.count - 1
   var indexPaths: [AnyObject] = []
   for i in 0...lastIdx {
             indexPaths.append(NSIndexPath(forItem: i, inSection: 0))
   }

   //Convert the new messages in modelbridge
   var messages = [ModelMessageBridge]()

   for message in history {
      messages.append(ModelMessageBridge(message: message))
   }

   // insert messages and update data source
   self.collectionView.insertItemsAtIndexPaths(indexPaths)
   self.JSQmessages.insert(messages, atIndex: 0) //error here