newb question on strings

R

regex_jedi

ok, I have looked a lot of places, and can't seem to get a clear
answer...

I have a string called
each_theme

Some values of the string may contain a single quote as in -
Happy
Sad
Nice
Frank's Laundry
Explosion

Notice that the 4th value has a single quote in it. Well, I need to
make sure that the single quote is escaped before handing it off for
further processing to a class I later call for some other processing.

So I thought, no big deal, I should be able to find a way to escape
the single quote on the string. I am a perl and PHP guy, so I do a
lot of regex stuff. I did a quick search and found someone had said
to use this re.sub function, so I tried. But the following doesn't
work. To be honest, I am a little lost with all the modules and
classes required to do simple math or string functions in Python.
None of it seems built it in.. its all import modules... Here is what
I am trying...

# escape single quotes in theme name
re.sub('''(['"])''', r'\\\1', each_theme)

you python masters... show me the way please...

thanks
regex_jedi
 
S

shandy.b

Are you trying to escape for a regular expression?

Just do re.escape().
Frank\'s\ Diner

If you're escaping for URLs, there's urllib2.quote(), for a command
line, use subprocess.list2cmdline.

Generally, the module that consumes the string should provide a
function like escape().


ok, I have looked a lot of places, and can't seem to get a clear
answer...

I have a string called
   each_theme

Some values of the string may contain a single quote as in -
   Happy
   Sad
   Nice
   Frank's Laundry
   Explosion

Notice that the 4th value has a single quote in it. Well, I need to
make sure that the single quote is escaped before handing it off for
further processing to a class I later call for some other processing.

So I thought, no big deal, I should be able to find a way to escape
the single quote on the string.  I am a perl and PHP guy, so I do a
lot of regex stuff.  I did a quick search and found someone had said
to use this re.sub function, so I tried.  But the following doesn't
work. To be honest, I am a little lost with all the modules and
classes required to do simple math or string functions in Python.
None of it seems built it in.. its all import modules...  Here is what
I am trying...

        # escape single quotes in theme name
        re.sub('''(['"])''', r'\\\1', each_theme)

you python masters... show me the way please...

thanks
regex_jedi
 
D

Dan Bishop

Are you trying to escape for a regular expression?

Just do re.escape().


Frank\'s\ Diner

If you're escaping for URLs, there's urllib2.quote(), for a command
line, use subprocess.list2cmdline.

And if you're escaping for string literals in Python (or C and its
descendants), you can do:
Frank\'s Diner
 
B

Bruno Desthuilliers

Dan Bishop a écrit :
And if you're escaping for string literals in Python (or C and its
descendants), you can do:

Frank\'s Diner

And finally, if you're escaping it to use it for a SQL query, any DB-API
compliant adapter will automatically escape it's params if used correctly.
 
C

Cédric Lucantis

Le Tuesday 24 June 2008 22:27:33 regex_jedi, vous avez écrit :
ok, I have looked a lot of places, and can't seem to get a clear
answer...

I have a string called
each_theme

Some values of the string may contain a single quote as in -
Happy
Sad
Nice
Frank's Laundry
Explosion

Notice that the 4th value has a single quote in it. Well, I need to
make sure that the single quote is escaped before handing it off for
further processing to a class I later call for some other processing.

So I thought, no big deal, I should be able to find a way to escape
the single quote on the string. I am a perl and PHP guy, so I do a
lot of regex stuff. I did a quick search and found someone had said
to use this re.sub function, so I tried. But the following doesn't
work. To be honest, I am a little lost with all the modules and
classes required to do simple math or string functions in Python.
None of it seems built it in.. its all import modules... Here is what
I am trying...

# escape single quotes in theme name
re.sub('''(['"])''', r'\\\1', each_theme)

No python has no builtin support for regexp like perl. There's nothing wrong
with your code, you just need to import the 're' module. Add this at the
beginning of your script:

import re

But imho escaping the quote in the first part would be more readable than
using triple quotes:
name = "Frank's Laundry"
re.sub(r"([\"'])", r"\\\1", name)
"Frank\\'s Laundry"

You'll find a list of all the standard modules in the python docs, including
this one:

http://docs.python.org/modindex.html
http://docs.python.org/lib/module-re.html
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top