473,320 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

from string to raw string

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
Jul 18 '05 #1
7 1931
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
Jul 18 '05 #2
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" <ex****@gmail.com> wrote in message
news:cp**********@eeyore.INS.cwru.edu...
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

Jul 18 '05 #3
Phd
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

Jul 18 '05 #4
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

Jul 18 '05 #5
On 14 Dec 2004 05:11:07 -0800, rumours say that
ch********@spritenote.co.uk 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...
Jul 18 '05 #6
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
Sc***********@Acm.Org
Jul 18 '05 #7
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" <Sc***********@Acm.Org> wrote in message
news:41******@nntp0.pdx.net...
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
Sc***********@Acm.Org

Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

467
by: mike420 | last post by:
THE GOOD: 1. pickle 2. simplicity and uniformity 3. big library (bigger would be even better) THE BAD:
9
by: Derek Hart | last post by:
I wish to execute code from a string. The string will have a function name, which will return a string: Dim a as string a = "MyFunctionName(param1, param2)" I have seen a ton of people...
40
by: Peter Row | last post by:
Hi all, Here is my problem: I have a SQL Server 2000 DB with various NVarChar, NText fields in its tables. For some stupid reason the data was inserted into these fields in UTF8 encoding. ...
5
by: Gregg | last post by:
Hello all, I have been banging my head over a problem that I am having reading a comma seperated file (CSV) that can contain from 1 to 10,000 records. My code snipit is as follows: **Start...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
18
by: James Radke | last post by:
Hello, We are currently using a user DLL that when working in VB 6.0 has a user defined type as a parameter. Now we are trying to use the same DLL from a vb.net application and are having...
0
by: Cleo | last post by:
Hi, I am trying to call a WebService Method written in Weblogic from VB.NET and I am getting the following error. I am using SOAP Caal s from VB.NET. Please find the wsdl file and my code. ...
3
by: Lumpierbritches | last post by:
I have an application my partner wrote that would allow an autoresponse to any Mapi compliant email that apparently in .Net won't, can someone assist me with fixing this? Here is the code: ...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.