在Python 3中,如何在单独的.txt文件中搜索字符串?

时间:2011-11-14 21:43:00

标签: python database string search text

我正在尝试在Python 3中编写一个代码,允许用户输入.txt文件的名称。然后用户输入关键字或短语,Python搜索.txt文件并返回该短语所在的行。此外,程序会将一些内容写入.html文件,但是一旦我弄清楚这一部分,我就可以处理它。到目前为止,这是我的代码:

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()

1 个答案:

答案 0 :(得分: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)匹配“端口”但不匹配“重要”。