Raw strings to normal strings conversion?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Aug 23 05:42:50 EDT 2007


On Thu, 23 Aug 2007 09:21:40 +0000, Nagarajan wrote:

> On Aug 23, 1:21 pm, James Stroud <jstr... at mbi.ucla.edu> wrote:
>> Nagarajan wrote:
>> > Is there a way by which I could obtain normal string form of a raw
>> > string.
>> > XML CDATA is returned as raw string. But I need the string to actually
>> > escape special chars.
>>
>> > Any idea?
>>
>> This doesn't seem clear. Perhaps an example of what you get and what you
>> want it converted to.
> 
> Here is an example:
>>> rawstr = r'a\nb'
>>> print rawstr
> a\nb
> 
> Now I need this newstr to actually interpret '\n', in other words, to
> behave like a normal string.

So you get a string with Newlines as two character sequence \n.  You don't
get "raw" strings.  That is a concept in Python source code.  When the
program is running there is no such distinction between "raw" and "normal"
strings.  Here's a solution:

In [87]: print r'a\nb'
a\nb

In [88]: print r'a\nb'.decode('string-escape')
a
b

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list