Multiple string replacement...

Simon Brunning SBrunning at trisystems.co.uk
Fri Sep 22 08:18:59 EDT 2000


I'm trying to put together a script which will replace values in files. The
idea is that you can specify multiple replacements at once. For example you
can replace 'Spam' with 'eggs' and 'Jesus' with 'Brian' in one pass.

So, I read the replacement sets in from a file, and store them in a list of
lists, thus:

replacements = [['Spam', 'eggs,], ['Jesus', 'Brian']]

Then I read in my file data, and do:

for replacement in replacements:
        filedata = filedata.replace(replacement[0], replacement[1])

This all works OK, except...

I set up a ROT13 replacement set, which looks like:
[['A', 'N'], ['B', 'O'], ['C', 'P'], ['D', 'Q'], ['E', 'R'], ['F', 'S'],
['G', 'T'], ['H', 'U'], ['I', 'V'], ['J', 'W'], ['K', 'X'], ['L', 'Y'],
['M', 'Z'], ['N', 'A'], ... and so on. The problem here is that after 'A'
and been replaced by 'N', it gets replaced back again.

Anyone got a cunning plan?

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk

P.S. Sorry about the legal gibberish below - out of my hands, I'm afraid. I
wouldn't mind so much, except for the poor grammar...




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.



More information about the Python-list mailing list