Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Translate escaped characters in a string

Reply
Thread Tools

Translate escaped characters in a string

 
 
Ben Finney
Guest
Posts: n/a
 
      04-02-2004
On 2 Apr 2004 15:57:59 -0800, - wrote:
> (\n is slash+n, and not LF)


No, "\n" is backslash + n. Slash + n would be "/n".

> I would like to use \n and other supported escape sequences when I
> write the text to a file.


Once you know the proper name for the character you're talking about,
you can find modules like this one:

<http://www.crazy-compilers.com/py-lib/shellwords.html>

which may be helpful.

--
\ "The best is the enemy of the good." -- Voltaire |
`\ |
_o__) |
Ben Finney <http://bignose.squidly.org/>
 
Reply With Quote
 
 
 
 
-
Guest
Posts: n/a
 
      04-02-2004
Hi

I have an application that reads the arguments sent to the script. If
an user enters a text like "Line 1\nLine2" (\n is slash+n, and not
LF), I would like to use \n and other supported escape sequences when
I write the text to a file.

Is there a way to do this, other than writing my own parser?
 
Reply With Quote
 
 
 
 
Josiah Carlson
Guest
Posts: n/a
 
      04-03-2004
> I have an application that reads the arguments sent to the script. If
> an user enters a text like "Line 1\nLine2" (\n is slash+n, and not
> LF), I would like to use \n and other supported escape sequences when
> I write the text to a file.
>
> Is there a way to do this, other than writing my own parser?


I know there's an easier way to do it, but the below works...
>>> import encodings
>>> print encodings.codecs.escape_decode('hello\\nworld')[0]

hello
world
>>>


If the stings are escaped normally, the above will produce what you want.

- Josiah
 
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
How to convert Unicode string to raw string escaped with HTML Entities ldng Python 3 05-10-2007 09:37 AM
convert string to the "escaped string" Petr Jakes Python 3 11-12-2005 05:32 PM
Convert Raw Text Escaped Characters to Characters nicholas.wakefield@gmail.com Java 2 07-11-2005 09:17 PM
Translating escaped characters Henrik S. Hansen Python 2 11-02-2004 07:12 AM
how to convert a string to an escaped string Gregory Toomey Perl Misc 7 02-12-2004 01:25 PM



Advertisments