473,808 Members | 2,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

newb question on strings

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
Jun 27 '08 #1
4 1038
Are you trying to escape for a regular expression?

Just do re.escape().
>>print re.escape('Happ y')
Happy
>>print re.escape("Fran k's Diner")
Frank\'s\ Diner

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

Generally, the module that consumes the string should provide a
function like escape().
On Jun 24, 1:27*pm, regex_jedi <kim.gen...@gma il.comwrote:
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
Jun 27 '08 #2
On Jun 24, 4:04*pm, "shand...@gmail .com" <shand...@gmail .comwrote:
Are you trying to escape for a regular expression?

Just do re.escape().
>print re.escape('Happ y')
Happy
>print re.escape("Fran k's Diner")

Frank\'s\ Diner

If you're escaping for URLs, there's urllib2.quote() , for a command
line, use subprocess.list 2cmdline.
And if you're escaping for string literals in Python (or C and its
descendants), you can do:
>>print "Frank's Diner".encode(' string-escape')
Frank\'s Diner

Jun 27 '08 #3
Dan Bishop a écrit :
On Jun 24, 4:04 pm, "shand...@gmail .com" <shand...@gmail .comwrote:
>Are you trying to escape for a regular expression?

Just do re.escape().
>>>>print re.escape('Happ y')
Happy
>>>>print re.escape("Fran k's Diner")
Frank\'s\ Diner

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

And if you're escaping for string literals in Python (or C and its
descendants), you can do:
>>>print "Frank's Diner".encode(' string-escape')
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.
Jun 27 '08 #4
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

--
Cédric Lucantis
Jun 27 '08 #5

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

Similar topics

8
2726
by: Travis Ray | last post by:
Hi, I just started a java class and I have this assignment and I'm stumped... I realize the code is messy... but I just started... so go easy on me :) Java Problem I am working on this assignment. The user enters a loan amount, number of years to repay, and annual percentage rate. The project is supposed to output a table for each month's payment information. I wrote the attached code and it works well. However, there is a lot of...
3
1564
by: Amy G | last post by:
I have a whole bunch of tuples that look something like this, aTuple = ('1074545869.6580.msg', 't_bryan_pw@gmcc.ab.ca', 'Your one stop prescriptions') now that I have this I try for x, y, z in aTuple: do something with x do something with y
2
1174
by: Mitch | last post by:
when using CIN How do i make it so user input can be 5 or 6 letters and how do i put input into an if else statement ? Example, i want the user to be able to input a word then program to decide what word to spit out. char input; cout<<"Hi "; cin>>input
24
2382
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 ); And this is what I wrote:
3
1196
by: Blue Man | last post by:
hello I am writing an error handling within different classes in asp.net. the error class reports the Exception E's string to the database. but it cuts big strings, if i just display the error it is ok , but when i send it to function to store it in database just a little part of error string appears. is that because of size of string?cuz when i change the error message by hand and put some shorter strings it works fine. i just declared...
4
1268
by: ProvoWallis | last post by:
I'm totally stumped by this problem so I'm hoping someone can give me a little advice or point me in the right direction. I have a file that looks like this: <SC>APPEAL<XC>40-24; 40-46; 42-46; 42-48; 42-62; 42-63 <SC>PROC GUIDE<XC>92<LT>1(b)(1) (i.e., <<SC><XC><SC><XC><LT>
14
1508
by: manstey | last post by:
Hi, Is there a clever way to see if two strings of the same length vary by only one character, and what the character is in both strings. E.g. str1=yaqtil str2=yaqtel they differ at str1 and the difference is ('i','e') But if there was str1=yiqtol and str2=yaqtel, I am not interested.
3
1373
by: kevinwolfe | last post by:
Hi all. I'd like any suggestions on how I can get my data set (not a DataSet) bound to a couple of controls on a form. Let me start by describing what my data looks like. Each entry correlates to a piece of equipment and has the following information: Two strings, one for a system name and one for equipment name An integer An ArrayList (which will contain only strings)
6
11318
by: johnny | last post by:
How do I join two string variables? I want to do: download_dir + filename. download_dir=r'c:/download/' filename =r'log.txt' I want to get something like this: c:/download/log.txt
3
2124
by: code_berzerker | last post by:
Hi i'm relatively new to Python and my C/C++ knowledge is near to None. Having said that I feel justified to ask stupid questions :) Ok now more seriously. I have question refering to char* used as function parameters to return values. I have read SWIG manual to find best way to overcome that, but there are many warnings about memory leaks and stuff, so I feel confused. Ok to put it more simply: how to safely define a variable in...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9195
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.