[Linux-bruxelles] Fonction addslashes sous Python ?

Jean-Christophe jchrbeumier at scarlet.be
Sam 28 Jan 19:01:49 CET 2006


Robert Viseur a écrit :

>Bonjour,
> 
>Existe-t-il un équivalent sous Python à la fonction addslashes en PHP (et de son inverse stripslashes) ?
>Elle ajoute des \ dans les chaines de caractères.
>
>Merci d'avance, 
>Robert. 
>  
>
google(addslashes python) donne une adresse exploitable:

Irene Ponti wrote:
 > Now in php exist a standard function that add slashes
 > in front of ' or " in a variable and another that
 > remove slashes.
 >
 >Is there in python a function like this?

Hi Irene,

you have 2 choices:
- string objects in python offers a replace() member.
   so you can write:
	query = """a string with 'quotes' and "doublequotes" """
  	new_query = query.replace('"', '\\"')	

- the re module allows for regular expression searching and replacement
   but this mail is far too short too explain regular expression.
   just have a look at the python manuals (chapter 4.2 of the library
   reference).

Just beware that '\' is a special character inside python string:
"\\'" is totally different from "\'". the first form describe a 
backslash preceding a single quote character, the second describe a 
string which only contains a single quote. idem for double quotes.

-- 
Jean-Christophe Beumier

N'oubliez pas de signer la pétition contre la publicité sexiste 'Pecephobie':
http://www.porteouverte.be/ginette.htm





Plus d'informations sur la liste de diffusion Linux-bruxelles