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

可紊 提交于 2019-12-04 06:24:23

问题


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)

回答1:


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.




回答2:


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




回答3:


#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()



来源:https://stackoverflow.com/questions/37902523/search-multiple-strings-from-file-in-a-file-and-print-the-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!