Curious to see alternate approach on a search/replace via regex

Peter Otten __peter__ at web.de
Fri Feb 8 10:22:11 CET 2013


Serhiy Storchaka wrote:

> On 07.02.13 11:49, Peter Otten wrote:
>> ILLEGAL = "-:./?&="
>> try:
>>      TRANS = string.maketrans(ILLEGAL, "_" * len(ILLEGAL))
>> except AttributeError:
>>      # python 3
>>      TRANS = dict.fromkeys(map(ord, ILLEGAL), "_")
> 
> str.maketrans()

D'oh.

ILLEGAL = "-:./?&="
try:
    maketrans = str.maketrans
except AttributeError:
    # python 2
    from string import maketrans
TRANS = maketrans(ILLEGAL, "_" * len(ILLEGAL))
...




More information about the Python-list mailing list