Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Translating escaped characters

Reply
Thread Tools

Translating escaped characters

 
 
Henrik S. Hansen
Guest
Posts: n/a
 
      11-01-2004
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)
 
Reply With Quote
 
 
 
 
Steven Bethard
Guest
Posts: n/a
 
      11-01-2004
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

 
Reply With Quote
 
 
 
 
Henrik S. Hansen
Guest
Posts: n/a
 
      11-02-2004
Steven Bethard <(E-Mail Removed)> 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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell command needs whitespace characters escaped metaperl Python 3 12-08-2006 06:30 PM
servlet request handling - escaped characters in request Thea Java 2 09-22-2006 12:08 PM
Convert Raw Text Escaped Characters to Characters nicholas.wakefield@gmail.com Java 2 07-11-2005 09:17 PM
Minidom output of XML escaped characters Derek Basch Python 2 03-25-2005 05:25 PM
Translate escaped characters in a string Ben Finney Python 2 04-03-2004 07:46 AM



Advertisments