Multiple string replacement...
dfan at harmonixmusic.com
Fri Sep 22 16:00:41 CEST 2000
Simon Brunning <SBrunning at trisystems.co.uk> writes:
| for replacement in replacements:
| filedata = filedata.replace(replacement, replacement)
| 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 | http://www.dfan.org
Honest Bob CD now available! | http://www.dfan.org/honestbob/cd.html
More information about the Python-list