How do I get a slice of a string held in a tuple?

Lorenzo lorenzo at excitement.com
Sun Apr 8 19:02:25 EDT 2007


In article <mailman.6185.1176056511.32031.python-list at python.org>,
 Georg Brandl <g.brandl at gmx.net> wrote:

> Lorenzo schrieb:
> 
> >> > How do I go about it?
> >> 
> >> Do it correctly. Post your actual example that fails
> >> and the related error message. Possibnly your indexes
> >> were out of range.
> >> 
> >> > I googled this and found a couple
> >> > of references, but no solution.
> >> 
> >> Well, there wouldn't be  a solution to a non-existent
> >> problem, would there?
> >> 
> >> > TIA
> > 
> > Here's the code:
> > 
> >  elapsedTime = mydata[1]
> >  index = elapsedTime.find("real")
> >  # the index will have a value 0f 110 
> >  totaltime = elapsedTime[index:]
> >  # instead of this returning a shortened html string, i only 
> >  # get the left angle bracket '<'
> 
> May it be that mydata[1] doesn't contain "real" at all? In that case,
> find() returns -1, and the slice elapsedTime[-1:] always contains
> at most one character.
> 
> If you replace "find" by "index", you get a ValueError exception if
> "real" was not found, if that helps you.
> 
> Whenever one calls str.find(), one has to check the return value for -1.
> 
> Georg

Oops! I sent the wrong piece of code. The above is actually the work 
around which actually works. The bad code is this:
index  = mydata[0].find("real")
elapsedTime = mydata[0][index:]

My apologies, but this is what fails.

-- 
"My Break-Dancing days are over, but there's always the Funky Chicken"
--The Full Monty



More information about the Python-list mailing list