簡體   English   中英

Python:腳本在文本文件中找不到單詞

[英]Python: Script won't find word in text file

我正在嘗試從文本文件中找到特定的單詞,但是我的腳本似乎無法將單詞與文本文件中一行所寫的內容相匹配,即使我知道它可以匹配。 我已經注意到有空格,但是既然我說的entry in line ,那行不行嗎?

我也嘗試過:

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

但它們似乎都不起作用

我看不到我要去哪里錯了。 任何幫助,將不勝感激。

這是我的代碼

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

使用打印語句(僅針對第一個條目),我看到:

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

文本文件如下所示(注意:我無法更改文本文件,因為這是我將收到的方式, '表示空白行):

'
'
Manufacturer=manufacturer_data
Model=model_data
'
'
'

更新:將我的腳本更改為:

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"

結果被打印出來(仍然沒有“在行中找到條目”):

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

將我的代碼更改為此:

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!!!!!!!!!!!!!"

給我這個:

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

您在第一個循環之后讀取到文件末尾。 相反,請交換循環,以便在文件的每一行都檢查System_data每個entry

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

或者您可以通過for line in f之前調用f.seek(0)在當前代碼中更正此行為

您應該從文件的條目和行中刪除所有空格/換行符。 因此,請在所有內容前加上前綴

entry = entry.strip()

並更改

if entry in line:

if entry in line.strip():

編輯:還有,摩西·科萊多耶(Moses Koledoye)說的

好的,所以看來問題在於該字符串實際上是十六進制形式。 但是當我使用print repr(line)時,它只以十六進制形式出現: '\\x00m\\x00a\\x00n\\x00u\\x00f\\x00a\\x00c\\x00t\\x00u\\x00r\\x00e\\x00r\\x00_\\x00d\\x00a\\x0‌​0t\\x00a\\x00'

所以我將代碼更改為以下內容:

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"

現在, if entry in line:輸入以下內容,此腳本將進入循環if entry in line:並顯示"Found entry in line"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM