Search Multiple Strings (from File) in a file and print the line

后端 未结 3 1402
無奈伤痛
無奈伤痛 2021-01-23 22:13

Again apologies for been noob here: Trying below code for searching multiple strings read from keywords and search in f and printing the line. It works if I have on

3条回答
  •  囚心锁ツ
    2021-01-23 22:41

    One of the challenges of looking for keywords is defining what you mean by keyword and how a file's contents should be parsed to find the full set of keywords. If "aa" is a keyword, should it match "aaa" or maybe ""aa()"? Can a keyword have numbers in it?

    A simple solution is to say that keywords are alphabetic only and should match contiguous alphabetic strings exactly, ignoring case. Further, matches should be considered line by line, not sentence by sentence. We can use a regex to find alphabetic sequences and sets to check containment like so:

    keys.txt

    aa bb 
    

    test.txt

    aa is good
    AA is good
    bb is good
    cc is not good
    aaa is not good
    

    test.py

    import re
    
    keyfile = "keys.txt"
    testfile = "test.txt"
    
    keys = set(key.lower() for key in 
        re.findall(r'\w+', open(keyfile , "r").readline()))
    
    with open(testfile) as f:
        for line in f:
            words = set(word.lower() for word in re.findall(r'\w+', line))
            if keys & words:
                print(line, end='')
    

    Result:

    aa is good
    AA is good
    bb is good
    

    Add more rules for what you mean by a match and it gets more complicated.

    EDIT

    Suppose you have one keyword per line and you just want a substring match (that is, "aa" matches "aaa") instead of a keyword search, you could do

    keyfile = "keys.txt"
    testfile = "test.txt"
    
    keys = [key for key in (line.strip() for line in open(keyfile)) if key]
    
    with open(testfile) as f:
        for line in f:
            for key in keys:
                if key in line:
                    print(line, end='')
                    break
    

    But I'm just guessing what your criteria are.

提交回复
热议问题