parsing tabs in a string

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon Sep 23 19:21:34 CEST 2002


On Monday 23 September 2002 10:11, Andrew Alzner wrote:
> Hello,
>
> I'm getting to know Python and have a quick question for something I don't
> understand.
>
> I'm trying to parse information extracted from a spreadsheet which is tab
> separated. Here is a simplified version of what I have:
>
> list=['"103-01a-17"\t2\t33\t256\t227\n']
> y=list[0]
> print y.find("\t")
> print y.find("\t",1)
>
> I thought the second .find should return the second occurence of "\t". To
> get the index of the second tab I have to type:
>
> print y.find("\t",13)
>
> This doesn't make sense to me. Can someone help me please.
>

>>> s = 'I      am      a       string'
>>> s.find('\t')
1
>>> s.find('\t',2)
4
>>> s[4]
'\t'
>>> s[3]
'm'

the second number tells find() where to start looking.  It uses the same 
notation as slicing.

>>> s.find('\t', 2, 4)
-1
>>> s.find('\t', 2, 5)
4

so for your code just catch the number find() returns, add one and continue.




More information about the Python-list mailing list