parsing tabs in a string

Kaleb Pederson kibab at
Mon Sep 23 13:29:47 EDT 2002

If you don't specify where find should start searching, it ALWAYS starts
at the beginning of the line and looks for the first occurrence of the
given substring.  Hence,"\t" is always found at that character. The
documentation clearly states that so you might want to double check it
if you aren't getting the desired behavior.


You might try:


>>> line = 'this\tis\ta\ttest\tof\t"mydatehere"'

>>> newline = line.split('\t')

>>> print newline

['this', 'is', 'a', 'test', 'of', '"mydatehere"']

>>> for x in newline:

...     print x









That will break list into several lists where each column is whatever
was in between your tab markers.

Or, if you really want to go through and find all the '\t's.. then.



>>> mystr = 'this\tis\ta\ttest\tof\t"mydatehere"'

>>> index = mystr.find('\t')

>>> while index != -1:

...     print 'index found at:',index

...     index = mystr.find('\t',index+1)


index found at: 4

index found at: 7

index found at: 9

index found at: 14

index found at: 17


Hopefully that helps.



-----Original Message-----
From: python-list-admin at [mailto:python-list-admin at]
On Behalf Of Andrew Alzner
Sent: Monday, September 23, 2002 10:11 AM
To: python-list at
Subject: parsing tabs in a string




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:


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.




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list