Translating escaped characters

Robert Brewer fumanchu at amor.org
Mon Nov 1 17:53:59 EST 2004


Henrik S. Hansen wrote:
> How do you best go about translating characters like '\\n' to '\n'?
> This is for a configuration file parser, where the "backslash
> convention" is supported.
> 
> The naive approach -- re.sub('\\\\(.)', '\\\1', s) -- doesn't work, of
> course.  The best I've come up with so far is a special case for every
> character to be translated.  There must be an easier way?

It's difficult to tell exactly what you're trying to end up with, but
raw strings are probably the answer you're looking for. In general, you
should form regexes with raw strings to keep you brain from exploding.

>>> r"\\n"
'\\\\n'
>>> re.sub(r'\\(.)', r'\1', r"\\n")
'\\n'


>From the docs:
If you're not using a raw string to express the pattern, remember that
Python also uses the backslash as an escape sequence in string literals;
if the escape sequence isn't recognized by Python's parser, the
backslash and subsequent character are included in the resulting string.
However, if Python would recognize the resulting sequence, the backslash
should be repeated twice. This is complicated and hard to understand, so
it's highly recommended that you use raw strings for all but the
simplest expressions.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list