reg exp and octal notation

Jeff Epler jepler at unpythonic.net
Fri Mar 5 10:49:44 EST 2004


If you have a string and want to perform backslash-substitution on it,
use python2.3's "string_escape" codec.

Two examples:

>>> s = "\\n"
>>> s
'\\n'
>>> s.decode("string_escape")
'\n'

>>> "\x30"
'0'
>>> "\\x30"  
'\\x30'
>>> "\\x30".decode("string_escape")
'0'

You can remove the trailing newline this way:
    if s.endswith("\n"): s = s[:-1]

Jeff




More information about the Python-list mailing list