从带有参数

时间:2016-11-19 22:30:01

标签: python database python-3.x

尝试编写一个程序,该程序将查看包含NYTimes畅销书的文本文件,并让用户从列表中搜索并打印书籍。

目前有这段代码打印包含他们输入的图书名称的行:

def lookup(): 
    file= input("Please enter the text file path: ")
    search = input("Enter the books you wish to search for:   ")
    search = [word.strip() for word in search.lower().split(",")]
    with open(file, 'r') as f:
        for line in f:
            line = "\n" + line  
            for word in search:
                if word.lower() in line.lower():
                    print(line)

lookup()

如何让用户搜索年份范围? txt文件包含书名,作者,发布年份等,均由1个标签空间分隔。

1 个答案:

答案 0 :(得分:1)

如何使用Pandas read_csv函数?

import pandas as pd

#reads in the tab delimited file
df = pd.read_csv(file, delimiter ='\t')

#gives all rows where the book year is 2000 or 2001
#You can pass the list of years generated by the user to the isin() function
df.loc[df.loc[:,'Year'].isin([2000, 2001]),:]

此解决方案当然假定您可以将文件读入内存。