如何搜索文本文件并返回包含用户输入字符串的所有项目

时间:2018-06-18 19:35:46

标签: python string python-3.x

文本文件在每一行上按字母顺序排列所有畅销书籍的书名,作者,出版商,日期和流派。我希望能够通过作者或标题搜索此文本文件,以便能够与作者一起打印所有书籍(&#34;作者的书名&#34;),其中包含已输入的字符串。< / p>

def author_search():
    author_string = input('Enter a title')
    lower_author = author_string.lower()
    with open('file.txt', 'r') as book_file:
        for line in book_file:
            if 'lower_title' in line:
                print(line)


def title_search():
    title_string = input('Enter a title')
    lower_title = title_string.lower()
    with open('file.txt', 'r') as book_file:
        for line in book_file:
            if 'lower_title' in line:
                print(line)

我没有收到任何错误,但是当我使用我的主要功能运行时,我也没有得到任何输出

def main():
    run = True
    while run:
        book_file = open('file.txt', 'r')
        print('What would you like to do? ')
        print('1: Look up year range')
        print('2:Look up month/year')
        print('3:Search for author')
        print('4:Search for title')
        print('Q: Quit')
        userOp = input('Select an operation by entering its number: ')
        book_file = open('file.txt', 'r')
        if userOp == 1:
            print(year_range(book_file))
        if userOp == 2: 
            print(month_year(book_file))
        elif userOp == 3:
            print(author_search(book_file))
        elif userOp == 4:
            print(title_search(book_file))
     runCheck = input('Do you want to run another operation? ')
        if runCheck.lower == 'Q':
            run = False
            print('Goodbye')

它要求我选择并操作,然后立即询问我是否要运行另一个操作。

A Dance With Dragons    George R. R. Martin Bantam   7/31/2011   Fiction
A Day Late and a Dollar Short   Terry McMillan  Viking  2/4/2001    Fiction
A Feast For Crows   George R. R. Martin Bantam  11/27/2005  Fiction
A Lion Is In the Streets    Adria Locke Langley McGraw  7/1/1945    Fiction
A Man In Full   Tom Wolfe   Farrar, Straus & Giroux 11/22/1998  Fiction

0 个答案:

没有答案