Text Parsing - character at a time...

Peter Hansen peter at engcorp.com
Fri Jul 9 07:58:21 EDT 2004


Fuzzyman wrote:

> I'm parsing character by character and adding the characters of the
> input string to the output until I come to ones I want to modify. 
[...]
> What's an efficient equivalent in python ? 

I think the answer might depend on information you haven't provided.
How are you 'parsing' this?  In other words, how do you know
when you've gotten to the "ones you want to modify"?  Are you sure
there isn't a way of avoiding the one-by-one manual scan?  If
there is, you can probably add characters whole sequences at a
time to a list, as you suggest, by slicing from the input string
once you know where a sequence of "kept" characters starts
and stops.

For that matter, could you use string.replace or re.sub to do
the job even more efficiently?

-Peter



More information about the Python-list mailing list