Need better string methods
Max M
maxm at mxm.dk
Mon Mar 8 08:52:08 EST 2004
benjamin schollnick wrote:
>>line = "..../bgref/stats.stf| SPICE | 3.2.7 | John Anderson \n"
>
> I am assuming your running into these lines on a regular basis, so
> make a wrapper around your python function... Call it "Cleanup" or
> "Parse_bar_line_string" or something that makes sense to your
> users, and have them call that function....
Or if it needs to be a bit more flexible, and the data allways follow
that recursive pattern in your line, it could be a recursive function::
def recursive_split(data, *args)
That would be used like::
result = recursive_split(data, '|', '.')
So that the string would build nested lists that are recursively split
on the arguments in the order they are passed.
In fact I wonder why such a function isn't a string method anyway, as
that approach is used so often for parsing simple files. Most of the
times I use split, I use it recursively on the input file. Ie for a
small tab seperated text file it would be:
data = """
a:1\tb:2\tc:3
d:4\te:5\tf:6
g:7\th:8\th:9
"""
result = recursive_split(data, '\n', '\t', ':')
for line in result:
for item in line:
key, value = item
regards Max M
More information about the Python-list
mailing list