[Tutor] Tab delimited question
Joel Goldstick
joel.goldstick at gmail.com
Mon Dec 13 21:23:57 CET 2010
On Mon, Dec 13, 2010 at 1:55 PM, Ben Ganzfried <ben.ganzfried at gmail.com>wrote:
> 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:
>
> mystring = "this is a key test123 noise noise noise
> noise noise"
>
you can get the words in a list with
mylist = mystring.split()
You can get the index of 'key' with
mylist.index('key')
So the next value is the one you want
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "this is a key test123 noise noise noise
noise noise"
>>> a = s.split()
>>> a
['this', 'is', 'a', 'key', 'test123', 'noise', 'noise', 'noise', 'noise',
'noise']
>>> a.index('key')
3
>>> mystuff = a[a.index('key') + 1]
>>> mystuff
'test123'
>>>
>
> 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
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101213/b5272c31/attachment.html>
More information about the Tutor
mailing list