How do I match a word in a text file using python?

前端 未结 5 1739
青春惊慌失措
青春惊慌失措 2021-01-13 03:58

I want to search and match a particular word in a text file.

with open(\'wordlist.txt\', \'r\') as searchfile:
        for line in searchfile:
            if         


        
相关标签:
5条回答
  • 2021-01-13 04:30

    split the line into tokens: if word in line.split():

    0 讨论(0)
  • 2021-01-13 04:32

    You ought to use a regular expression. The regular expression howto from the Python docs might be a good place to start.

    0 讨论(0)
  • 2021-01-13 04:40

    You can always use regex, something along the lines of:

    import re
    
    with open('wordlist.txt', 'r') as searchfile:
            for line in searchfile:
                if re.search( r'\sthere\s', line, re.M|re.I):
                        print line
    
    • \sthere\s - any space followed by 'there' followed by any space
    • re.I - means case insensitive
    • re.M - doesn't really matter in this case (since lines only have 1 \n)
    0 讨论(0)
  • 2021-01-13 04:48

    Look up the re module (regular expressions). re.search with the regex ' there ' is what you want.

    0 讨论(0)
  • 2021-01-13 04:53
    import re
    
    file = open('wordlist.txt', 'r')
    
    for line in file.readlines():
        if re.search('^there$', line, re.I):
            print line
    

    The re.search function scans the string line and returns true if it finds the regular expression defined in the first parameter, ignoring case with re.I. The ^ character means 'beginning of the line' while the $ character means 'end of the line'. Therefore, the search function will only return true if it matches there preceded by the beginning of the line, and followed by the end of the line, aka isolated on its own.

    0 讨论(0)
提交回复
热议问题