Problems replacing \ with \\

S

samslists

Hi...

Here's a weird problem...I'm trying to escape a bunch of data to put
into a database.

Here's what I have:

def escape(string):
"""
Escape both single quotes and blackslashes 'fun\\\\fun'
"""
string = string.replace('\\', '\\\\')
return string

Now the commands in the doctest work when I type them by hand into the
python interpreter!'fun\\\\fun'


But they don't work when I actually run them with doctest:
Failed example:
escape(x)
Expected:
'fun\\fun'
Got:
'fun\x0cun'

Why?

Thanks!
 
M

MRAB

Hi...

Here's a weird problem...I'm trying to escape a bunch of data to put
into a database.

Here's what I have:

def escape(string):
"""
Escape both single quotes and blackslashes
'fun\\\\fun'
"""
string = string.replace('\\', '\\\\')
return string

Now the commands in the doctest work when I type them by hand into the
python interpreter!>>> x = r"fun\fun"

'fun\\\\fun'

But they don't work when I actually run them with doctest:
Failed example:
escape(x)
Expected:
'fun\\fun'
Got:
'fun\x0cun'

Why?

Thanks!

Your doctest is in a triple-quoted string which contains the line:
^^

which is the same as:
^^^^

If you wrap a raw string in just quotes that is isn't a raw string any
longer!

HTH
 
O

ockman

HTH --

Thank you for the response. I'm not sure I understand the last
sentence, although I think I get the idea. How do I create a proper
doctest?

Thanks
 
M

Michael Torrie

Hi...

Here's a weird problem...I'm trying to escape a bunch of data to put
into a database.

Is it possible to use the database API and prepared statements to avoid
having to go through this exercise? Also, most database APIs work
natively in unicode, so creating your prepared statement and then
passing in each parameter as an argument saves a lot of hassle, since
the API's usually decode the unicode strings automatically.
 
G

Gabriel Genellina

(top posting corrected)

Thank you for the response. I'm not sure I understand the last
sentence, although I think I get the idea. How do I create a proper
doctest?

r"This is a raw string"

"""
r"This is NOT a raw string"
"""

r"""
r"This is a raw string too"
"""

What matters are the OUTER quotes.

Now, why do you want to escape the text yourself? Assuming you have a
DBAPI compatible module, use bound parameters when you execute the query:

cursor.execute("insert ... values (?,?,?)", (name, address, year))

Those ? are placeholders for the actual values; no explicit quoting on the
values is required (the module itself, or the database, takes care of it).
Not all databases support the ? style, there are other ways. See
http://www.python.org/dev/peps/pep-0249/ for the DB API specification.
 
S

samslists

Sorry for the late response. I've been travelling internationally and
am just getting back to work.

So--thank you to everyone who responded!

To answer everyone's question---- I am dumping all of the data from a
mysql database, then creating a postgresql database, then inserting
the data into the new postgresql database.

This seems to be a reasonable way to do it, because all I have to do
is double my 's and my \s (I hope).

Thanks
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top