Parsing a file based on differing delimiters

Alex Martelli aleaxit at yahoo.com
Tue Oct 21 18:59:59 EDT 2003


Kylotan wrote:

> I have a text file where the fields are delimited in various different
> ways. For example, strings are terminated with a tilde, numbers are
> terminated with whitespace, and some identifiers are terminated with a

What sadist designed it?-)  Anyway...

I suggest a simple class which holds the filedata and an index into
it.  Your functions such as GetWord(f) examine f.data from f.index
onwards, and increment f.index before returning the result.  To
"pushback", you just decrement f.index again (you may want to
keep a small stack of values - perhaps just one -- for the "undo",
again in the simple class in question).


Alex





More information about the Python-list mailing list