Multiple string replacement...

Dan Schmidt dfan at
Fri Sep 22 16:00:41 CEST 2000

Simon Brunning <SBrunning at> writes:

| 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.

I don't have a solution to the general problem of multiple conflicting
string replacement, but if all you're trying to do is multiple
character replacement, as in this example, use string.maketrans() and
string.translate(), which were made for exactly this sort of problem.

                 Dan Schmidt |
Honest Bob CD now available! |

More information about the Python-list mailing list