473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problems replacing \ with \\

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
>>x = r"fun\fun"
escape(x)
'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"
escape(x)
'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!
Jun 27 '08 #1
5 1225
On Apr 21, 11:48 pm, "samsli...@gmail.com" <samsli...@gmail.com>
wrote:
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
>>x = r"fun\fun"
>>escape(x)
'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"
>escape(x)

'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:
>>x = r"fun\fun"
^^

which is the same as:
>>x = r"fun\x0cun"
^^^^

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

HTH
Jun 27 '08 #2
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

On Apr 21, 9:08 pm, MRAB <goo...@mrabarnett.plus.comwrote:
On Apr 21, 11:48 pm, "samsli...@gmail.com" <samsli...@gmail.com>
wrote:
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
>>x = r"fun\fun"
>>escape(x)
'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"
>>escape(x)
'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:
>x = r"fun\fun"

^^

which is the same as:
>x = r"fun\x0cun"

^^^^

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

HTH
Jun 27 '08 #3
sa*******@gmail.com wrote:
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.
Jun 27 '08 #4
(top posting corrected)

En Mon, 21 Apr 2008 21:12:44 -0300, oc****@gmail.com <oc****@gmail.com>
escribió:
def escape(string):
"""
Escape both single quotes and blackslashes
>>x = r"fun\fun"
>>escape(x)
'fun\\\\fun'
"""
string = string.replace('\\', '\\\\')
return string
Failed example:
escape(x)
Expected:
'fun\\fun'
Got:
'fun\x0cun'

Your doctest is in a triple-quoted string which contains the line:
>>x = r"fun\fun"
which is the same as:
>>x = r"fun\x0cun"

If you wrap a raw string in just quotes that is isn't a raw string any
longer!
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.

--
Gabriel Genellina

Jun 27 '08 #5
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
On Apr 21, 7:12*pm, Michael Torrie <torr...@gmail.comwrote:
samsli...@gmail.com wrote:
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.
Jun 27 '08 #6

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

Similar topics

9
by: please-answer-here | last post by:
What is wrong with this code? when i place "return false" before the xfab assignment the form as intended doesn't submit. But when placed as here or in the last if/else construct the page gets...
3
by: Muntz | last post by:
Hi - we are MSDN Enterprise subscribers, our no-spam address is munter@computershare.com We are currently trying to replace a SoapToolkit web-service with a .NET WebMethod implementation, but...
0
by: Jawor | last post by:
Hi, I try to make Polish Language Instalator for my Web application. I have some problems with replacing english text with polish content. For example, on the Welcome Screen I have: "Welcome to ...
10
by: Shawn | last post by:
Hi, For a few years, I have been developing each of my clients websites using a seperate web site (unique IP) to solve problems with relative URL's between my local dev station and the...
3
by: Srinivas | last post by:
Hi, I am using ASP.NET SmtpMail to send email using the following code public void SendEmail(string From, string To, string Subject, StringBuilder Message, string Attachment, string...
3
by: Jeff | last post by:
I've been working on an application for a while now that has been giving me some trouble when it comes to working with a picturebox and memory usage. My company deals with digital imaging, so we...
20
by: John | last post by:
I have a page that requests an index number from the user using a form. The submit then calls itself and a php lookup table determines the web page required and then does a jump. echo...
2
by: Gary Coutts | last post by:
Hi, I am have problems redirecting from a login page. The login page is simple, with just 2 textboxes and one button. On the button click the routine below is called: I am using: Visual...
7
by: JV | last post by:
I am running a large Access 2000 application (installed using Access Runtime) that uses many VB SendKeys, primarily to expand combo boxes On Enter. When trying to run it on Vista I get a...
1
by: patelgaurav85 | last post by:
Hi, I want to convert xml in one format into another xml format shown below Input xml : <Name> <Name1> <Name11>Name11</Name11> <Name12>Name12</Name12>
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.