1

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?

3
  • cardsDoc = wordsDoc? Commented Jan 5, 2014 at 4:56
  • Do you want the input string to match the entire line or just part of it? Commented Jan 5, 2014 at 5:03
  • sorry, cardsDoc = wordsDoc yes. I fixed that. op_text is the input string that I need to search. and I want to see if there is a substring in op_text that matches an entire line in the file, then save that substring. Commented Jan 5, 2014 at 20:50

3 Answers 3

1

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()]
Sign up to request clarification or add additional context in comments.

3 Comments

I think the OP is trying to find if the key is in a substring of the line, not if it matches the entire line.
I think the opposite. OP says substring that matches an entire line. It's entirely unclear and we should ask OP to clarify.
I need to find a substring in op_text that matches an entire line in the FILE
1

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

Comments

1

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...

3 Comments

It looks like when you are matching the entire line against the entire input string. what I need to do is match a substring of the input text to an entire line in the file. so if the input text has any mention of a line in the file, it will save that line
I showed you how to do both. For a complete match, use if string == line.lower() to test if the line contains the match string, use if string in line.lower() you state that 'I want to be able to search an input string for a substring that matches an entire line in the file'
I beleive you meant if line.lower() in string since I want a substring in the input to match a line, but when I did this it never even entered the for loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.