[Tutor] Tab delimited question

Ben Ganzfried ben.ganzfried at gmail.com
Mon Dec 13 19:55:21 CET 2010


I'm searching line by line for certain tags and then printing the tag
followed by the word immediately following the tag.

So for example, suppose I had the following line of text in a file:
"this	is	a	key	test123	noise	 noise	noise 	noise 	noise"

In this example, I would want to print "key test123" to a new file.
The rest of the words I would not want.

Here is my code so far:

def test(infile, outfile):
  for line in infile:
            tagIndex = line.find("key")
            start = tagIndex + 4
            stop = line[start:].find("\t") -1
            if tagIndex != -1:
                print("start is: ", start)
                print("stop is: ", stop)
                print("spliced word is ", line[start: stop])

My question is the following: What is wrong w/ the variable 'stop'?
The index it gives me when I print out 'stop' is not even close to the
right number.  Furthermore, when I try to print out just the word
following the tag w/ the form: line[start: stop], it prints nothing
(it seems b/c my stop variable is incorrect).

I would greatly appreciate any help you have.  This is a much
simplified example from the script I'm actually writing, but I need to
figure out a way to eliminate the noise after the key and the word
immediately following it are found.

Thank you very much for any help you can provide.

Ben


More information about the Tutor mailing list