2016-11-19 1 views
0

NYTimes 베스트셀러 도서가 포함 된 텍스트 파일을 살펴보고 사용자가 목록에서 책을 검색하고 인쇄 할 수있게하는 프로그램을 작성하려고합니다.매개 변수가있는 텍스트 파일의 Python 인쇄

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 탭 간격으로 구분되어 있습니다.

+0

범위를 검색하는 것은 다소 어렵지만 FWIW는 [csv] (https://docs.python.org/3/library/csv.html) 모듈을 사용하여보다 편리하게 데이터를 읽을 수 있습니다. 나는 당신의 파일을 사전 목록, 책 당 한 사전으로 읽는 것이 좋습니다. –

답변

1

팬더 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]),:] 

물론이 솔루션은 파일을 메모리로 읽을 수 있다고 가정합니다.

관련 문제