parsing tabs in a string

Kaleb Pederson kibab at icehouse.net
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

...

this

is

a

test

of

"mydatehere"

 

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.

 

--Kaleb

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

 

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.

 

Thanks,

Andrew

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020923/21a61394/attachment.html>


More information about the Python-list mailing list