2011-11-14 89 views
0

我想在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() 
+5

擺脫所有與問題無關的代碼。 – mikerobi

+0

聽起來奇怪的功課有關...?如果是這樣,請標記爲。 – JAL

回答

1

它看起來像你缺少的部分是如何搜索「TXT」文件。這段代碼可以幫助您入門:

import glob 

keyword = raw_input('Enter a keyword to search for: ') 
for filename in glob.glob('*.txt'): 
    with open(filename) as f: 
     for lineno, line in enumerate(f, 1): 
      if keyword in line: 
       print repr(keyword), 'found on line', lineno, 'in', filename, ':', repr(line) 

注意,該if keyword in line是開始最簡單的方法,但它不是一個完善的過濾器(在「端口」的搜索將匹配「重要」)。

更復雜的方法將使用正則表達式(例如,re.search(r'\bport\b', line)將匹配「端口」,但不是「重要」)。

+0

你可以像這樣在文件標識符上使用'enumerate()'嗎?我想你會需要使用'枚舉(f.readlines(),1)'。 – aganders3

+2

是的。文件對象充當迭代器(例如:用於f中的行),並且可以在任何可以使用迭代器的地方使用。 –

+0

感謝您的快速響應。我會開始玩弄你給我的東西,看看會發生什麼。 –