Raw strings to normal strings conversion?

Discussion in 'Python' started by Nagarajan, Aug 23, 2007.

  1. Nagarajan

    Nagarajan Guest

    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?
    Nagarajan, Aug 23, 2007
    #1
    1. Advertising

  2. Nagarajan

    James Stroud Guest

    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.

    In the meantime, see if urllib.unquote() doesn't do what you need.

    James
    James Stroud, Aug 23, 2007
    #2
    1. Advertising

  3. Nagarajan

    Nagarajan Guest

    On Aug 23, 1:21 pm, James Stroud <> 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.
    >
    > In the meantime, see if urllib.unquote() doesn't do what you need.
    >
    > James


    And yes, unquote doesn't help.
    Nagarajan, Aug 23, 2007
    #3
  4. On Thu, 23 Aug 2007 09:21:40 +0000, Nagarajan wrote:

    > On Aug 23, 1:21 pm, James Stroud <> 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
    Marc 'BlackJack' Rintsch, Aug 23, 2007
    #4
  5. Nagarajan

    Nagarajan Guest

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

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Walter L. Preuninger II

    literal escape sequence conversion to raw

    Walter L. Preuninger II, Jan 5, 2004, in forum: C Programming
    Replies:
    6
    Views:
    477
    Kevin Goodsell
    Jan 5, 2004
  2. Gaurav Gupta
    Replies:
    0
    Views:
    283
    Gaurav Gupta
    Oct 20, 2003
  3. Bill Janssen
    Replies:
    2
    Views:
    760
    Michael Hudson
    Mar 2, 2004
  4. Rune Froysa
    Replies:
    3
    Views:
    573
    Rune Froysa
    Apr 20, 2005
  5. NevilleDNZ
    Replies:
    1
    Views:
    602
    NevilleDNZ
    Jan 1, 2007
Loading...

Share This Page