CrazyBurrito
CrazyBurrito

Reputation: 115

Search for a string from file in python

I am reading a file with a different string on each line. I want to be able to search an input string for a substring that matches an entire line in the file and then save that substring so that it can be printed. This is what I have right now:

wordsDoc = open('Database.doc', 'r', encoding='latin-1')
words = wordsDoc.read().lower()
matching = [string for string in words if string in op_text]

But this matches on each character. How would I do this properly?

Upvotes: 1

Views: 106

Answers (3)

dawg
dawg

Reputation: 104092

Couple of comments:

First, using with to open a file is usually better:

with open('Database.doc', 'r', encoding='latin-1') as f:
    # closes the file automagically at the end of this block...

Second, there is no need to read in the whole file unless you are doing something with the file as a whole. Since you are searching lines, deal with the lines one by one:

matches=[]
with open('Database.doc', 'r', encoding='latin-1') as f:
    for line in f:
        if string in line.lower():
             matches.append(line)

If you are trying to match the entire line:

matches=[]
with open('Database.doc', 'r', encoding='latin-1') as f:
    for line in f:
        if string == line.lower():
             matches.append(line)

Or, more Pythonically, with a list comprehension:

with open('Database.doc', 'r', encoding='latin-1') as f:
    matches=[line for line in f if line.lower()==string]

etc...

Upvotes: 1

Burhan Khalid
Burhan Khalid

Reputation: 174708

I assume the idea is that there is some search phrase and if it is contained in any line from the file, you want to filter those lines out.

Try this, which will compare the lower cased version of the line, but will return the original line from the file if it contains the search_key.

with open('somefile.doc') as f:
   matching = [line for line in f if search_key in line.lower()]

Upvotes: 1

kojiro
kojiro

Reputation: 77167

This will create a list named "matching" containing all the lines in the file that exactly match the string in op_text, once lowercased.

with open('Database.doc', 'r', encoding='latin-1') as wordsDoc:
    matching = [line for line in wordsDoc if op_text == line.lower()]

Upvotes: 1

Related Questions