简体   繁体   中英

Python: Script won't find word in text file

I am trying to find specific words from a text file, however my script doesn't seem to be able to match the word to what's written on a line in the text file, even though I know it matches. I've noticed there are spaces but since I am saying entry in line , shouldn't it work?

I have also tried:

  if str(entry) in line:, 
  if str(entry) in str(line): and 
  if entry in str(line): 

but none of them seem to work either

I can't see where I'm going wrong. Any help would be appreciated.

Here is my code

with open(address+'file_containing_data_I_want.txt') as f:
    for entry in System_data:
        print "Entry:"
        print entry 
        for line in f:
            print "Start of line"
            print line
            print"End of line"
            if entry in line:
                print "Found entry in line" #This never gets printed

Using the print statements (for just the first entry) I see:

Entry:
Manufacturer


Start of line
??

End of line
Start of line


End of line
Start of line
Manufacturer=manufacturer_data

End of line
Start of line
Model=model_data

End of line
Start of line


End of line
Start of line


End of line

The text file looks like this (Note:I can't change the text file as this is the way I will be receiving it, ' indicates a blank line):

'
'
Manufacturer=manufacturer_data
Model=model_data
'
'
'

UPDATE: Changing my script to:

with open(address+'file_containing_data_I_want.txt') as f:
    for line in f:
        print "Start of line %s" % line
        print"End of line" 
        for entry in System_data:
            print "Entry: %s" % entry
            if entry in line.strip():
                print "Found entry in line"

Results in this being printed (Still no "Found entry in line"):

Entry: Manufacturer
Entry: Model
Start of line: 
End of line
Entry: Manufacturer
Entry: Model
Start of line: Manufacturer=manufacturer_data
End of line
Entry: Manufacturer
Entry: Model
Start of line: Model=model_data
Entry: Manufacturer
Entry: Model
Start of line: 
End of line
Entry: Manufacturer
Entry: Model
Start of line: 
End of line

Changing my code to this:

for line in f:
    print "Start of line: %s" % line.strip("\r\n")
    print "End of line" 
    for entry in System_data:
        print "Entry: %s" % entry.strip()
        if entry.strip() in line.strip("\r\n"):
            print "FOUND!!!!!!!!!!!!!"

Gives me this:

Start of line: ??
End of line
Entry: Manufacturer
Entry: Model
Start of line: 
End of line
Entry: Manufacturer
Entry: Model
Start of line: Manufacturer=manufacturer_data
End of line
Entry: Manufacturer
Entry: Model
Start of line: Model=model_data
End of line

You read to the end of the file the after the first loop. Swap the loops instead, so each entry in System_data gets checked at each line of the file:

for line in f:
    print "Start of line %s" % line
    print "End of line" 
    for entry in System_data:
        print "Entry: %s" % entry
        if entry.strip() in line.strip("\r\n"):
            print "Found entry in line" #This now gets printed

or you can correct this behavior in your current code by calling f.seek(0) before for line in f

You should strip all blanks/newlines from both the entry and lines in file. So, prefix everything with

entry = entry.strip()

and change the

if entry in line:

to

if entry in line.strip():

EDIT: also, what Moses Koledoye says

Ok so it seems the issue was that the string was actually in hexadecimal form. But it only appeared in hexadecimal form to me when I used print repr(line) it appeared like: '\\x00m\\x00a\\x00n\\x00u\\x00f\\x00a\\x00c\\x00t\\x00u\\x00r\\x00e\\x00r\\x00_\\x00d\\x00a\\x0‌​0t\\x00a\\x00'

So I changed my code to the following:

with open(address+'file_containing_data_I_want.txt') as f:
    for line in f:
        for entry in System_data:
            line=line.strip()
            line = re.sub(r'[^\w=]', '', line)
            if entry in line:
                print "Found entry in line"

This script now enters the loop if entry in line: and prints "Found entry in line"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM