#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Posts
    4
    Rep Power
    0

    Searching for a list of strings in a file with Python


    Hi guys,

    I'm trying to search for several strings, which I have in a .txt file line by line, on another file.
    So the idea is, take input.txt and search for each line in that file in another file, let's call it rules.txt.

    So far, I've been able to do this, to search for individual strings:

    Code:
    import re
    
    shakes = open("output.csv", "r")
    
    for line in shakes:
        if re.match("STRING", line):
            print line,
    How can I change this to input the strings to be searched from another file?

    So far I haven't been able to.

    Thanks for the ideas.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    Use this program instead.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    16
    Rep Power
    0
    patterns = open("input.txt")
    lines = open("rules.txt")

    for pattern in patterns:
    for line in lines:
    if re.match(pattern, line):
    print line

    There ya go!

IMN logo majestic logo threadwatch logo seochat tools logo