Text Manipulation in Python

Donn Cave donn at u.washington.edu
Wed Nov 10 12:06:44 EST 1999


Quoth Edward Hasted <edward at corpex.com>:
| We want to use it to alter specific lines in template files, typically 
| something like changing:-
|
| Variable = 1234
|
| to 
|
| Variable = Company Name
|
| The text manipulation strings within Python seem to work sequentially 
| rather than on a line basis.
|
| 1. Is this correct?

Hard to say, that analysis may be a little too terse.

| 2. What is the best way to do text searching and manipulation within 
| Python.

Depends!  I thought the system Thomas Fuchs proposed in his followup
was interesting, and if it works for you, perhaps that's close enough
to the best - but there isn't enough detail here to know whether it's
suited to your needs at all.

Another followup mentioned that perhaps the best way is to not do it
in Python, and there's something to that too.  Certainly on UNIX, or
platforms like BeOS that borrow from its repertoire, there are an
embarrassment of riches in text manipulation languages, and it's a
point of tradition that the language or tool to solve each little
problem be considered separately (I don't mean that to sound silly,
either, but will not expound further here.)

In a project of mine, on BeOS, I've been using "m4", a macro language,
to do what you seem to have in mind.  Most of the heavy lifting is done
in Python, but the result goes into a template processed by m4.  It's
kind of like the C preprocessor but much more capable, and despite its
relative obscurity, usually it's already there on a normal UNIX platform,
in my experience anyway.

	Donn Cave, University Computing Services, University of Washington
	donn at u.washington.edu




More information about the Python-list mailing list