escape single and double quotes

Kent Johnson kent37 at tds.net
Thu Mar 24 18:02:48 EST 2005


Leif B. Kristensen wrote:
> I'm working with a Python program to insert / update textual data into a
> PostgreSQL database. The text has single and double quotes in it, and I
> wonder: What is the easiest way to escape quotes in Python, similar to
> the Perlism "$str =~ s/(['"])/\\$1/g;"?

Just for the record (even though it's not the right solution to your problem), the Python equivalent is
   re.sub('''(['"])''', r'\\\1', s)

Kent



More information about the Python-list mailing list