Saadi381
Saadi381

Reputation: 55

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

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 only one keyword but not if I have more then one.

keywords = input("Please Enter keywords path as c:/example/ \n :")
keys = open((keywords), "r").readline()
with open("c:/saad/saad.txt") as f:
    for line in f:
        if (keys) in line:
            print(line)

Upvotes: 1

Views: 18337

Answers (3)

Harshan Gowda
Harshan Gowda

Reputation: 177

#The Easiest one...
def strsearch():

  fopen = open('logfile.txt',mode='r+')

  fread = fopen.readlines()

  x = 'Product Name'

  y = 'Problem Description'

  z = 'Resolution Summary'

  for line in fread:

      #print(line)

       if x in line:

           print(line)

       if y in line:

           print(line)

       if z in line:

           print(line)

strsearch()

Upvotes: 0

joel goldstick
joel goldstick

Reputation: 4493

keywords = input("Please Enter keywords path as c:/example/ \n :")
keys = open((keywords), "r").readline()
keys = keys.split(',')  # separates key strings
with open("c:/saad/saad.txt") as f:
    for line in f:
        for key in keys:
            if key.strip() in line:
                print(line)

You are reading the line in as one string. You need to make a list of each comma separated string. Then test each key for each line (removing whitespace around the key)

This is assuming your keyword file is something like: aa is good, bb is good, spam, eggs

Upvotes: 0

tdelaney
tdelaney

Reputation: 77347

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.

Upvotes: 3

Related Questions