Ich versuche, bestimmte Wörter aus einer Textdatei zu finden, aber mein Skript scheint nicht in der Lage zu sein, das Wort mit dem zu vergleichen, was in einer Zeile in der Textdatei steht obwohl ich weiß, dass es passt. Ich habe bemerkt, dass es Leerzeichen gibt, aber da ich sage entry in line
, sollte es nicht funktionieren?Python: Skript findet kein Wort in Textdatei
habe ich auch versucht:
if str(entry) in line:,
if str(entry) in str(line): and
if entry in str(line):
aber keiner von ihnen scheinen entweder
ich sehen kann, nicht zu arbeiten, wo ich falsch gehe. Jede Hilfe wäre willkommen.
Hier ist mein 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
die Druckanweisungen (für nur den ersten Eintrag) Ich sehe:
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
Die Textdatei sieht wie folgt aus (Anmerkung: Ich kann nicht ändern die Textdatei, da dies die Art, wie ich es empfangen wird, '
eine leere Zeile an):
'
'
Manufacturer=manufacturer_data
Model=model_data
'
'
'
UPDATE: Ändern mein Skript:
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"
Ergebnisse in dieser gedruckt wird (noch nicht "Gefunden Eintrag 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
Ändern der Code folgendermaßen aus:
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!!!!!!!!!!!!!"
Gibt mir das:
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
Es scheint, als ob Ihr 'Eintrag' Zeilenumbrüche enthält. Versuchen Sie es in Ihrem 'if' zu entfernen. – Lafexlos