parsing tabs in a string
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
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.
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
I'm getting to know Python and have a quick question for something I
I'm trying to parse information extracted from a spreadsheet which is
tab separated. Here is a simplified version of what I have:
I thought the second .find should return the second occurence of "\t".
To get the index of the second tab I have to type:
This doesn't make sense to me. Can someone help me please.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list