from string to raw string

Discussion in 'Python' started by Dan Perl, Dec 14, 2004.

  1. Dan Perl

    Dan Perl Guest

    Is there a way to convert a regular string to a raw string so that one could
    get from '\bblah' to r'\bblah' other than parsing the string and modifying
    the escapes?

    I am interested in this for the use of regular expressions. I would like to
    be able to accept re patterns as inputs either from a file or from a GUI
    tool and I would like to allow those inputs as regular strings and not force
    users to write patterns with double escapes.

    I naively thought there could be utilities for such a conversion either in
    the string module or the StringIO module but I don't see any utilities like
    that.

    So, any suggestions?

    Dan
    Dan Perl, Dec 14, 2004
    #1
    1. Advertising

  2. Dan Perl

    Brian Beck Guest

    Dan Perl wrote:
    > Is there a way to convert a regular string to a raw string so that one could
    > get from '\bblah' to r'\bblah' other than parsing the string and modifying
    > the escapes?


    There's no such thing as a raw string, only raw string literals. In
    other words, it's a syntax to tell the Python interpreter which
    characters in your string are 'special' and has no effect on strings not
    input as literals directly within your code. Strings from files or any
    input besides the interactive Python shell will already be what you're
    looking for.

    --
    Brian Beck
    Adventurer of the First Order
    Brian Beck, Dec 14, 2004
    #2
    1. Advertising

  3. Dan Perl

    Dan Perl Guest

    Yeah, you're right. I got it all twisted in my mind. It's late and I must
    be getting tired.

    Thanks.

    Dan

    "Brian Beck" <> wrote in message
    news:cplqc9$4rj$...
    > Dan Perl wrote:
    >> Is there a way to convert a regular string to a raw string so that one
    >> could get from '\bblah' to r'\bblah' other than parsing the string and
    >> modifying the escapes?

    >
    > There's no such thing as a raw string, only raw string literals. In other
    > words, it's a syntax to tell the Python interpreter which characters in
    > your string are 'special' and has no effect on strings not input as
    > literals directly within your code. Strings from files or any input
    > besides the interactive Python shell will already be what you're looking
    > for.
    >
    > --
    > Brian Beck
    > Adventurer of the First Order
    Dan Perl, Dec 14, 2004
    #3
  4. Dan Perl

    Phd Guest

    Hi,

    I recently asked the same question, the response I got was that just use
    the string. There is no raw string object so the conversion doesn't
    exist. As far as I know, I haven't run into any problem

    Take a try. Please let me know if there is any problem with this approach.


    Good luck



    Dan Perl wrote:

    > Is there a way to convert a regular string to a raw string so that one could
    > get from '\bblah' to r'\bblah' other than parsing the string and modifying
    > the escapes?
    >
    > I am interested in this for the use of regular expressions. I would like to
    > be able to accept re patterns as inputs either from a file or from a GUI
    > tool and I would like to allow those inputs as regular strings and not force
    > users to write patterns with double escapes.
    >
    > I naively thought there could be utilities for such a conversion either in
    > the string module or the StringIO module but I don't see any utilities like
    > that.
    >
    > So, any suggestions?
    >
    > Dan
    >
    >
    Phd, Dec 14, 2004
    #4
  5. Dan Perl

    Guest

    Dan Perl wrote:
    > Yeah, you're right. I got it all twisted in my mind. It's late and

    I must
    > be getting tired.
    >


    Perl & Twisted in the same thread, that should fool the search engines
    :D
    , Dec 14, 2004
    #5
  6. On 14 Dec 2004 05:11:07 -0800, rumours say that
    might have written:

    >Dan Perl wrote:
    >> Yeah, you're right. I got it all twisted in my mind. It's late and
    >>I must be getting tired.


    >Perl & Twisted in the same thread, that should fool the search engines
    >:D


    Now, if only Sam Ruby appeared to post his opinion...
    --
    TZOTZIOY, I speak England very best.
    "Be strict when sending and tolerant when receiving." (from RFC1958)
    I really should keep that in mind when talking with people, actually...
    Christos TZOTZIOY Georgiou, Dec 14, 2004
    #6
  7. Dan Perl wrote:
    > Is there a way to convert a regular string to a raw string so that one could
    > get from '\bblah' to r'\bblah' other than parsing the string and modifying
    > the escapes?


    Assuming you might mean something else, that something else might be:

    s = r'no_tab_\t_here'
    len(s.split()) == 1
    len(s.decode('string_escape').split()) == 2
    s = 'tab_\t_here'
    len(s.split()) == 2
    len(s.encode('string_escape').split()) == 1

    -Scott David Daniels
    Scott David Daniels, Dec 15, 2004
    #7
  8. Dan Perl

    Dan Perl Guest

    This is not what I meant. My posting was a judgement error. You are right
    though that my intuition was leading me to something like this. However, I
    didn't realize that it was not necessary for what I was doing. But this is
    very educational too. It made me look up string decode, encode, and codecs
    in the library reference and I found out more about them. I'm sure it's
    going to be useful. Thanks!

    Dan

    "Scott David Daniels" <> wrote in message
    news:...
    > Dan Perl wrote:
    >> Is there a way to convert a regular string to a raw string so that one
    >> could get from '\bblah' to r'\bblah' other than parsing the string and
    >> modifying the escapes?

    >
    > Assuming you might mean something else, that something else might be:
    >
    > s = r'no_tab_\t_here'
    > len(s.split()) == 1
    > len(s.decode('string_escape').split()) == 2
    > s = 'tab_\t_here'
    > len(s.split()) == 2
    > len(s.encode('string_escape').split()) == 1
    >
    > -Scott David Daniels
    >
    Dan Perl, Dec 16, 2004
    #8
    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. Phd
    Replies:
    3
    Views:
    1,132
    Steven Bethard
    Dec 6, 2004
  2. ldng
    Replies:
    3
    Views:
    1,807
    Tim Golden
    May 10, 2007
  3. Mirko Dziadzka

    print string as raw string

    Mirko Dziadzka, Feb 17, 2009, in forum: Python
    Replies:
    5
    Views:
    388
    Miles
    Feb 17, 2009
  4. Chris Angelico
    Replies:
    3
    Views:
    151
    Mark Lawrence
    Mar 1, 2013
  5. Peter Otten
    Replies:
    0
    Views:
    131
    Peter Otten
    Feb 28, 2013
Loading...

Share This Page