我想在Python 3中編寫一個代碼,允許用戶輸入.txt文件的名稱。然後用戶輸入關鍵字或短語,Python搜索.txt文件並返回該短語所在的行。此外,該程序會將一些內容寫入.html文件,但一旦我弄清楚了這一部分,我可以照顧到這一點。這是我到目前爲止的代碼:在Python 3中,如何在單獨的.txt文件中搜索字符串?
def main(): # Main function
list_variable = 0
search_term = 0
line = 0
links = 0 #Set list variables search and links equal to empty
print("The Great Search Engine, by Jackson Reeves")#Print Intro
filename = input("Enter name of database file: ")#Ask for filename
infile = open(filename, "r") #Use read function to read file
keyword = filename.find(input("Enter keyword to search for: "))
for line in filename.readlines():
line = line + 1
url = "http://www.aol.com"
url2 = "http://www.toyota.com"
url3 = "http://www.uky.edu"
url4 = "http://www.google.com"
url5 = "http://www.eku.edu"
url6 = "http://www.yahoo.com"
url7 = "http://www.youtube.com"
url8 = "http://www.msn.com"
#if keyword exists in file, execute these statements
p1 = ("<html>\n")
out_file = open("mypage.html", "wt")
out_file.write(p1)
p2 = ("<head><title>Search Findings</title></head>\n")
out_file.write(p2)
p3 = ("<body>\n")
out_file.write(p3)
p4 = ('<h2><p align=center>Search for "'+keyword+'"</h2>\n')
out_file.write(p4)
p5 = ("<p align=center>\n")
out_file.write(p5)
p6 = ("<table border>\n")
out_file.write(p6)
p7 = ("<tr><th>Hit<th>URL</tr>\n")
out_file.write(p7)
#if keyword is in line one, add this statement
p8 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url+'"> '+url+'</a></tr>\n')
out_file.write(p8)
#if keyword is in line three, add this statement
p9 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url2+'"> '+url2+'</a></tr>\n')
out_file.write(p9)
#if keyword is in line five, add this statement
p10 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url3+'"> '+url3+'</a></tr>\n')
out_file.write(p10)
#if keyword is in line seven, add this statement
p11 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url4+'"> '+url4+'</a></tr>\n')
out_file.write(p11)
#if keyword is in line nine, add this statement
p12 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url5+'"> '+url5+'</a></tr>\n')
out_file.write(p12)
#if keyword is in line eleven, add this statement
p13 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url6+'"> '+url6+'</a></tr>\n')
out_file.write(p13)
#if keyword is in line thirteen, add this statement
p14 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url7+'"> '+url7+'</a></tr>\n')
out_file.write(p14)
#if keyword is in line thirteen, add this statement
p15 = ('<tr><td>search <b>"'+keyword+'"</b><td><a href="'+url8+'"> '+url8+'</a></tr>\n')
out_file.write(p15)
p16 = ("</table>\n")
out_file.write(p16)
p17 = ("</body>\n")
out_file.write(p17)
p18 = ("</html>\n")
out_file.write(p18)
out_file.close
#if no keyword exists in file, execute these statements
p19 = ("<html>\n")
out_file = open("mypage.html", "wt")
out_file.write(p19)
p20 = ("<head><title>Search Findings</title></head>\n")
out_file.write(p20)
p21 = ("<body>\n")
out_file.write(p21)
p22 = ('<h2><p align=center>Search for "'+keyword+'"</h2>\n')
out_file.write(p22)
p23 = ("<p align=center>\n")
out_file.write(p23)
p24 = ("<table border>\n")
out_file.write(p24)
p25 = ('<tr><td>'+keyword+' <td> not found </tr>\n')
out_file.write(p25)
p26 = ('</table>\n')
out_file.write(p26)
p27 = ('</body>\n')
out_file.write(p27)
p28 = ('</html>\n')
out_file.write(p28)
out_file.close
main()
擺脫所有與問題無關的代碼。 – mikerobi
聽起來奇怪的功課有關...?如果是這樣,請標記爲。 – JAL