473,231 Members | 1,752 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,231 software developers and data experts.

Escaping the semicolon?

Hi all,

Is this expected behavior?
>>s = '123;abc'
s.replace(';', '\;')
'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.

Thanks
Nick
Dec 4 '07 #1
6 12269
Nick a écrit :
Hi all,

Is this expected behavior?
>>>s = '123;abc'
s.replace(';', '\;')
'123\\;abc'
>>print s.replace(';', '\;')
123\;abc
I just wanted a single backslash.
You got it - even if it's not obvious !-)
I can see why this probably happens
but i wondered if it is definitely intentional.
>>s2 = '123\;abc'
s2
'123\\;abc'
>>print s2
123\;abc
>>list(s2)
['1', '2', '3', '\\', ';', 'a', 'b', 'c']

As you can see, '\\' is counted as a single character !-)
Since the backslash is the escape character, you need to escape it to
have a litteral backslash:
>>s3 = '\'
File "<stdin>", line 1
s3 = '\'
^
SyntaxError: EOL while scanning single-quoted string
>>>
Dec 4 '07 #2
Nick wrote:
Hi all,

Is this expected behavior?
>>>s = '123;abc'
s.replace(';', '\;')
'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.
There is only a single backslash. But the interactive prompt will use the
repr()-function to print out returned values. Which will for strings print
their escaped syntax.

Try the above with a

print s.replace(...)

and you will see your desired outcome.

diez
Dec 4 '07 #3
Is this expected behavior?
>
>>>s = '123;abc'
s.replace(';', '\;')
'123\\;abc'
You're asking the interpreter to print a representation of your
string, so it does so. Representations wrap the results in
quotes and escape characters within that need escaping.
>>s.replace(';', '\;')
'123\\;abc'
>>print repr(s.replace(';', '\;'))
'123\\;abc'
>>print s.replace(';', '\;')
123\;abc

Additionally, it's best-practice to use raw strings or literal
backslashes, making your replacement either

r'\;'

or

'\\;'

because depending on the character following the back-slash, it
may be translated as a character you don't intend it to be.

-tkc
Dec 4 '07 #4
Mel
Nick wrote:
Is this expected behavior?
>>>s = '123;abc'
s.replace(';', '\;')
'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.
What you're seeing on the screen is a "literalization" of the string
value for the sake of the display. Consider

Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>s = '123;abc'
b = s.replace(';', '\;')
b
'123\\;abc'
>>len(b)
8
The length suggests that there's only one backslash in the string.
Mel.

On the other hand
>>repr(b)
"'123\\\\;abc'"

Isn't what I expected. No, wait, it is. It's the value of repr(b)
repred by the Python display logic.

MPW
Dec 4 '07 #5
Thanks guys, you answered that interactive prompt question really
clearly however, whats going on here. This works now -
>>working_string = '123;abc'
search_string = ';'
print working_string.replace(search_string, '\\' + search_string)
123\;abc

But this doesn't -

---
import sys
import string

input = string.join(sys.argv[1:], '')
escape_char_list = [ '(', '[]', ';', '^', '\\', '/', '|', '*', '$',
'&', '[', ']', ')', '?' ]

for ch in escape_char_list:
input = input.replace(ch, '\\' + ch)

print input
---

Try "123 *?/ abc d;o /$'" as the argument... and you get -

123 \*\?\/ abc d\\;o \/\$

Still two back slashes, did i miss something very obvious? Sorry,
sometimes these things are exasperating and just need more eyes or a
head screwed on (if thats the case!).

Nick
Dec 4 '07 #6
>
If you move '\\' to the front of your list of replacement characters,
things will probably work as you expect.

--
Jerry
I knew it would be something like that! Thanks for your help.
Dec 4 '07 #7

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

Similar topics

4
by: Dave Moore | last post by:
Hi All, Can anybody point me to a FAQ or similar that describes what all this stuff is about please?. I'm interfacing with a MySQL database if that's relavent. I've read a couple of books which...
5
by: bobbyballgame | last post by:
I am having a problem calling Stored Procedures: .... dim MyValue, MyOtherValue MyValue = "Bobby's value" MyOtherValue = Bobby's other value" rs.Open "exec MyStoredProc """ & MyValue &...
3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
2
by: Mark Hansen | last post by:
Friends, I'm using db2sql92 to execute an SQL script that is performing an insert operation on a table in a DB2, version 8.2 database server. The string value contains embedded semicolon...
3
by: Dan | last post by:
I'm writing a record from an asp.net page to SQL Server. After the insert I'm selecting @@identity to return the ID of the record that I just wrote. It worked fine until I typed a semicolon into...
11
by: Geoff Caplan | last post by:
Hi folks, The thread on injection attacks was very instructive, but seemed to run out of steam at an interesting point. Now you guys have kindly educated me about the real nature of the issues,...
3
by: Peter Michaux | last post by:
Hi, These first three links say that when reading document.cookie the name-value pairs are separated by semicolons and they show examples like "name=value;expires=date" where there is clearly...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
1
by: David Henderson | last post by:
I know 'disable-output-escaping' has been discussed in the past, but I can't put my finger on any of the threads to see if my current problem is addressed. Sorry for re-asking the question if it...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.