Lange Rede, kurzer Sinn:
Ich möchte eine Applikation erstellen die eine Textdatei durchsucht nach einem Suchwort, das der Benutzer eingeben kann. rausgeben soll der dann:
1. wie oft das wort in der datei enthalten ist
2. Die Zeilen ausgeben in denen das Wort vorkommt
Bei mir ist's jetzt nur so, wenn in einer Zeile ein Wort zweimal vorkommen würde, würde die Applikation es nur einmal erkennen. da er nur die Zeilen zählt.
Code: Alles auswählen
file = open('story.txt', 'r')
text = file.readlines()
file.close()
for line in text:
print line, # print text
# count how often the keyword is in the text
keyword = raw_input("Search for a keyword: ")
count_key = 0
for line in text:
if keyword in line:
count_key = count_key + 1 # count keyword
print count_key, "matches of the keyword", keyword, "found."
raw_input("Press [ENTER] to continue.")
# print all sentences that include the keyword
print "\nThese lines include the keyword", keyword, ":"
lines = 0 # count sentences
for line in text: # for every dot. there should be a sentence
lines = lines + 1
if keyword in line: # print every sentece that includes the keyword
print lines,":", line
raw_input("Press [ENTER] to exit.")