Re: Raw strings to normal strings conversion?



On Aug 23, 2:42 pm, Marc 'BlackJack' Rintsch <bj_...@xxxxxxx> wrote:
On Thu, 23 Aug 2007 09:21:40 +0000, Nagarajan wrote:
On Aug 23, 1:21 pm, James Stroud <jstr...@xxxxxxxxxxxx> 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

Thanks a lot.

.



Relevant Pages

  • Re: Construct raw strings?
    ... > the interpreter, like: ... There is no such thing as a "raw string." ... literal has been interpreted to yield a string object, ...
    (comp.lang.python)
  • Re: When does the escape character work within raw strings?
    ... special meaning in raw string literals. ... characters, a backslash followed by an 'n'. ... have a special meaning to the sub function, ...
    (comp.lang.python)
  • Re: Raw Strings (I Think)
    ... puts a escape charater infront of the back slashes ... If you want to know that the string really contains, ... Btw, if you want to pass arguments to the program, you might want to use the "subprocess" module instead, since it handles escaping and quoting for you all by itself: ... > I think I need to convert my string to a raw string but ...
    (comp.lang.python)
  • Re: print string as raw string
    ... characters and the quote marks are also at issue. ... raise ValueError('No raw string representation; ...
    (comp.lang.python)
  • Re: Problems replacing with \
    ... > return string ... If you wrap a raw string in just quotes that is isn't a raw string any ... why do you want to escape the text yourself? ...
    (comp.lang.python)