Text Parsing - character at a time...
michael at foord.net
Fri Jul 9 13:46:29 CEST 2004
I want to parse some text and generate an output that is similar but
not identical to the input.
The string I produce will be of similar length to the input string -
but a bit longer.
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. This
means creating a new string for every character (since strings are
immutable) which seems very inneficient - particularly when I know
roughly what the output length will be. In a language like c I think I
could reserve a chunk of memory and keep a track of how much I'd
filled... just putting characters into it.(If I filled it I could
reserve a smaller chunk more - not difficult to keep a track of).
What's an efficient equivalent in python ? I could use a list,
appending characters onto the end of it.. converting to a string at
the end using ''.join(thelist).
More information about the Python-list