how to match substring in a string using python

This question already has an answer here:

  • Does Python have a string 'contains' substring method? 13 answers
  • Check if a Python list item contains a string inside another string 11 answers

  • 如果我正确理解你的问题,在这里你可以试试这个:

    players_list=['Cricket','PSL','IPL','t20','shahid afridi','aamer yamin']
    cur.execute("SELECT tweet FROM tweets_data")
    for row in cur.fetchall():
        if any(True for p in players_list if p.lower() in row.lower()):
            print(row)
        else:
            print ("Else statement")
    

    You seem to have a typo with Circket . Also, there is no need to use indices -- just loop over players_list directly, making sure that both the player and the row have a predictable case. Something like:

    players_list=['Cricket','PSL','IPL','t20','shahid afridi','aamer yamin']
    
    cur.execute("SELECT tweet FROM tweets_data")  # Query for getting specific attribute
    for row in cur.fetchall():
        for player in players_list: 
            if player.lower() in row.lower():
                print 'list item:', player
                print row
            else:
                print 'Else statement.'
    
    链接地址: http://www.djcxy.com/p/9386.html

    上一篇: 用包含方法修改python字符串

    下一篇: 如何使用python匹配字符串中的子串