(easy question) Find and replace multiple items

Mark Peters mpeters42 at gmail.com
Tue Aug 8 17:00:19 EDT 2006


ds4ff1z wrote:
> Hello, i'm looking to find and replace multiple characters in a text
> file (test1). I have a bunch of random numbers and i want to replace
> each number with a letter (such as replace a 7 with an f  and 6 with a
> d). I would like a suggestion on an a way to do this.  Thanks

how about:

>>> import string
>>> text1 = "foo bar 12 spam joe876"
>>> table = string.maketrans("0123456789","uydnwkdfpx")
>>> text1.translate(table)
'foo bar yd spam joepfd'

Mark Peters




More information about the Python-list mailing list