String to valid Python identifier

Vlastimil Brom vlastimil.brom at gmail.com
Sat Aug 8 09:21:38 EDT 2009


2009/8/8 Дамјан Георгиевски <gdamjan at gmail.com>:
>
>
>>> Is there any easy function in the stdlib to convert any random string
>>> in a valid Python identifier .. possibly by replacing non-valid
>>> characters with _ ?
...
>
> ps.
> by "convert" I didn't mean a full transformation... I was more hoping
> for the least intrusive transformation that would still leave
> discernible resemblance of the original string.
>
>
>
> --
> дамјан ( http://softver.org.mk/damjan/ )
>
> When you do things right, people won't be sure if you did anything at
> all.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Depending on the needs (speed, readability ...), the regular
expression replacement might be viable too:
>>> import re
>>> print re.sub(r"[^a-zA-Z0-9_]", r"_", u"aábc d:eé_123's - foo?!var")
a_bc_d_e__123_s___foo__var
>>> print re.sub(r"\W", r"_", u"aábc d:eé_123's - foo?!var") # equivalent
a_bc_d_e__123_s___foo__var
>>>

Additionally, as with the other approaches, you also have to check for
the (illegal) starting digit and replace it or prepend some valid
prefix.
hth
 vbr



More information about the Python-list mailing list