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 6 12325
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
>>>
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
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
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
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
>
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 &...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |