views:

3263

answers:

6

Update: My current question is how can I get my code to read to the EOF starting from the beginning with each new search phrase.

This is an assignment I am doing and currently stuck on. Mind you this is a beginner's programming class using Python.

jargon = open("jargonFile.txt","r")
searchPhrase = raw_input("Enter the search phrase: ")
while searchPhrase != "":
    result = jargon.readline().find(searchPhrase)
    if result == -1:
        print "Cannot find this term."
    else:
        print result
    searchPhrase = raw_input("Enter the search phrase: ")
jargon.close()

The assignment is to take a user's searchPhrase and find it in a file (jargonFile.txt) and then have it print the result (which is the line it occured and the character occurence). I will be using a counter to find the line number of the occurence but I will implement this later. For now my question is the error I am getting. I cann't find a way for it to search the entire file.

Sample run:

Enter the search phrase: dog
16
Enter the search phrase: hack
Cannot find this term.
Enter the search phrase:

"dog" is found in the first line however it is also found in other lines of the jargonFile (multiple times as a string) but it is only showing the first occurence in the first line. The string hack is found numerous times in the jargonFile but my code is setup to only search the first line. How may I go about solving this problem?

If this is not clear enough I can post up the assignment if need be.

+1  A: 

Your file is jargon, not jargonFile (a string). That's probably what's causing your error message. You'll also need a second loop to read each line of the file from the beginning until you find the word you're looking for. Your code currently stops searching if the word is not found in the current line of the file.

How about trying to write code that only gives the user one chance to enter a string? Input that string, search the file until you find it (or not) and output a result. After you get that working you can go back and add the code that allows multiple searches and ends on an empty string.

Update:

To avoid iterating the file multiple times, you could start your program by slurping the entire file into a list of strings, one line at a time. Look up the readlines method of file objects. You can then search that list for each user input instead of re-reading the file.

Kristo
I've modified the code. The error in the sample run still presists.
SD
+2  A: 

First you open the file and read it into a string with readline(). Later on you try to readline() from the string you obtained in the first step.

You need to take care what object (thing) you're handling: open() gave you a file "jargon", readline on jargon gave you the string "jargonFile".

So jargonFile.readline does not make sense anymore

Update as answer to comment:

Okay, now that the str error problem is solved think about the program structure:

big loop
  enter a search term
  open file
  inner loop
     read a line
     print result if string found
  close file

You'd need to change your program so it follows that descripiton

Update II:

SD, if you want to avoid reopening the file you'd still need two loops, but this time one loop reads the file into memory, when that's done the second loop asks for the search term. So you would structure it like

create empty list
open file
read loop:
    read a line from the file
    append the file to the list
close file
query loop:
    ask the user for input
    for each line in the array:
        print result if string found

For extra points from your professor add some comments to your solution that mention both possible solutions and say why you choose the one you did. Hint: In this case it is a classic tradeoff between execution time (memory is fast) and memory usage (what if your jargon file contains 100 million entries ... ok, you'd use something more complicated than a flat file in that case, bu you can't load it in memory either.)

Oh and one more hint to the second solution: Python supports tuples ("a","b","c") and lists ["a","b","c"]. You want to use the latter one, because list can be modified (a tuple can't.)

myList = ["Hello", "SD"]
myList.append("How are you?")
foreach line in myList:
    print line

==>

Hello
SD
How are you?

Okay that last example contains all the new stuff (define list, append to list, loop over list) for the second solution of your program. Have fun putting it all together.

froh42
I've modified the code. The error in the sample run still presists. The searchPhrase "hack" (str) is found all through out the file. My question is how do I go about searching the entire file, not just the first line.Thank you for your help.
SD
I've updated my answer in response to your comment.
froh42
I can see how your structure will work but I have to using a priming structure to achieve the results (reopening the file mulitple times is redundant as my professor will say). What should I look for in the second loop in respects to my posted code?
SD
The inner loop is iterating over the lines of the file. Currently, you're only checking one line for each term. You have to check them all.
recursive
+1  A: 

Everytime you enter a search phrase, it looks for it on the next line, not the first one. You need to re-open the file for every search phrase, if you want it behave like you describe.

muratgu
+1  A: 

Take a look at the documentation for File objects:

http://docs.python.org/library/stdtypes.html#file-objects

You might be interested in the readlines method. For a simple case where your file is not enormous, you could use that to read all the lines into a list. Then, whenever you get a new search string, you can run through the whole list to see whether it's there.

DNS
+1  A: 

Hmm, I don't know anything at all about Python, but it looks to me like you are not iterating through all the lines of the file for the search string entered.

Typically, you need to do something like this:

enter search string
open file
if file has data
   start loop
     get next line of file
     search the line for your string and do something

   Exit loop if line was end of file

So for your code:

jargon = open("jargonFile.txt","r")
searchPhrase = raw_input("Enter the search phrase: ")
while searchPhrase != "":
    <<if file has data?>>
      <<while>>
        result = jargon.readline().find(searchPhrase)
        if result == -1:
            print "Cannot find this term."
        else:
            print result
      <<result is not end of file>>
   searchPhrase = raw_input("Enter the search phrase: ")
jargon.close()

Cool, did a little research on the page DNS provided and Python happens to have the "with" keyword. Example:

with open("hello.txt") as f:
    for line in f:
        print line

So another form of your code could be:

searchPhrase = raw_input("Enter the search phrase: ")
while searchPhrase != "":
    with open("jargonFile.txt") as f:
        for line in f:
           result = line.find(searchPhrase)
           if result == -1:
              print "Cannot find this term."
           else:
              print result
    searchPhrase = raw_input("Enter the search phrase: ")

Note that "with" automatically closes the file when you're done.

Rake36
+1  A: 

you shouldn't try to re-invent the wheel. just use the re module functions. your program could work better if you used: result = jargon.read() . instead of: result = jargon.readline() . then you could use the re.findall() function and join the strings (with the indexes) you searched for with str.join() this could get a little messy but if take some time to work it out, this could fix your problem. the python documentation has this perfectly documented

Is there an example you can present me with this? I'd love to try it this way.
SD