Can't get search result for csv file in Python

For the assignment, I need to code a program that can search for a tab-delimited text file with specific values, i.e. search for the author in the author column and then print the entire row. Here is the code so far ...

import csv
import sys

#Assign File to "SearchFile"
SearchFile=raw_input(str("Enter the name of the file you want to search: "))

#open csv
reader = csv.reader(open(SearchFile, "rb"), delimiter="\t")

#Search Request
search_request = raw_input(str("Search on author (A=)or journal/conference (J=), [Q = quit]: "))

#Author Search
if search_request.startswith("A="):
    for row in reader:
        if search_request in row[0]:
            print row
        else:
            print("Sorry, could not be found")

      

I've seen some similar examples on stackoverflow, but still can't seem to solve my problem. I can get it to read the file, but can't find any of the search results? I'm new to Python, so if anyone can help it would be great!

Some lines of csv file:

AUTHOR(S)    YEAR    TITLE    JOURNAL/CONFERENCE
Accot;Zhai  2001    Scale effects in steering law tasks Proc. ACM CHI
Acredolo    1977    Developmental Changes in the Ability to Coordinate Perspectives of a Large-Scale Space  Developmental Psychology

      

+3


source to share


2 answers


At least one of the reasons this doesn't work is because you forgot that search_request has "A =". Clearly this is not what you mean: you want to search row[0]

for characters after "A =". So you need to uncheck the "A =" from search_request first ...



if search_request.starts_with("A="):
    seach_request = search_request[2:]  # strip off the selector "A="
    for row in reader:
        if search_request in row[0]:
            print row
        else:
            print("Sorry, could not be found")
else:
  print("Ooops, your selection (%s) is not supported right now" % search_result[:2])

      

+1


source


I think that line [0] is not prefixed with "A =". You should probably remove the prefix from the search_request string.



...
#Author Search 
if search_request.startswith("A="): 
    for row in reader: 
        if search_request[2:] in row[0]:
            print row
        else: 
            print("Sorry, could not be found")

      

+1


source







All Articles