The fundamentals...

Steve smnordby at yahoo.com
Fri Jan 26 18:03:32 EST 2001


mspiggie at my-deja.com wrote:

> I have been trying to find a way to use replace() to perform more than
> one replacement within a given file.  For instance, suppose I needed to
> replace ". " with "\n", but also need to replace "!" with "exclamation"
> and "?" with "question".  I have tried several approaches to this, but
> none of my neophyte ideas have produced satisfying results.
> 

You might try a dictionary:

subs = {'.': '\n', '!': 'exclamation', '?': 'question'}
for key in subs.keys():
    search = key
    replacement = subs[key]
    outfile = string.replace(infile, search, replacement)

-Steve-



More information about the Python-list mailing list