By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,659 Members | 940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,659 IT Pros & Developers. It's quick & easy.

Translating escaped characters

P: n/a
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?

--
Henrik S. Hansen http://freecode.dk/~hsh/
Computer science (software engineering) student
Free Software Foundation member #1702 (http://member.fsf.org)
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Henrik S. Hansen <hsh <at> freecode.dk> writes:

How do you best go about translating characters like '\\n' to '\n'?


Is this what you're looking for?
'\\n'.decode('string_escape') '\n' '\n'.encode('string_escape')

'\\n'

There's some info about it here:

http://docs.python.org/lib/node127.html

and searching Google Groups shows up a few things too:

http://groups.google.com/groups?q=escape+sequences&
meta=group%3Dcomp.lang.python.*

(Sorry, the URL wrapped.)

Steve

Jul 18 '05 #2

P: n/a
Steven Bethard <st************@gmail.com> writes:
How do you best go about translating characters like '\\n' to '\n'?


Is this what you're looking for?
'\\n'.decode('string_escape') '\n' '\n'.encode('string_escape')

'\\n'


Precisely, thank you.

--
Henrik S. Hansen http://freecode.dk/~hsh/
Computer science (software engineering) student
Free Software Foundation member #1702 (http://member.fsf.org)
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.