By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

find and remove "\" character from string

P: n/a
Hi,
i have the following string s and the following code, which doesn't
successfully remove the "\", but sucessfully removes the "\\".
>>s="Sad\\asd\asd"
newS=""
for i in s:
.... if i!="\\":
.... newS=newS+i
....
>>newS
'Sadasd\x07sd'

I have also read the following, but i do not understand the "...and the
remaining characters have been mapped through the given translation
table, which must be a string of length 256". Can some explain?

*translate*( table[, deletechars])

Return a copy of the string where all characters occurring in the
optional argument deletechars are removed, and the remaining
characters have been mapped through the given translation table,
which must be a string of length 256.

For Unicode objects, the translate() method does not accept the
optional deletechars argument. Instead, it returns a copy of the s
where all characters have been mapped through the given translation
table which must be a mapping of Unicode ordinals to Unicode
ordinals, Unicode strings or |None|. Unmapped characters are left
untouched. Characters mapped to |None| are deleted. Note, a more
flexible approach is to create a custom character mapping codec
using the codecs <http://docs.python.org/lib/module-codecs.html>
module (see encodings.cp1251 for an example).



Sep 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Konstantinos Pachopoulos wrote:
i have the following string s and the following code, which doesn't
successfully remove the "\", but sucessfully removes the "\\".
>>>s="Sad\\asd\asd"
newS=""
for i in s:
... if i!="\\":
... newS=newS+i
I'm not quite sure what you're trying to achieve, but I'd use
>>r"\\a\\b\c".replace("\\\\", "")
'ab\\c'
>>r"\\a\\b\c".replace("\\", "")
'abc'

Note that "\\" in the source is unescaped to "\" in the string. Use r"\\" to
prevent that.

Stefan
Sep 15 '07 #2

P: n/a
I V
On Sat, 15 Sep 2007 19:34:45 +0300, Konstantinos Pachopoulos wrote:
Hi,
i have the following string s and the following code, which doesn't
successfully remove the "\", but sucessfully removes the "\\".
There is no \\ in the string; there's one \ , which gets succesfully
removed.
>>s="Sad\\asd\asd"
When you write a string in the source code \\ gets changed to \ and \a
gets changed to "ASCII Bell (BEL)" (that's what the docs say), which is a
(non-printable) control code that is supposed to make the terminal beep.
>>newS=""
>>for i in s:
... if i!="\\":
Here, your test is true if i is not \
... newS=newS+i
...
>>newS
'Sadasd\x07sd'
And here, you have a string containing no backslashes, but containing a
character with ASCII code 7; it turns out that ASCII code 7 is the "ASCII
Bell", i.e., the character that you added to the string when you wrote
'\a'.
Sep 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.