find and remove "\" character from string

Konstantinos Pachopoulos kostaspaxos at yahoo.gr
Sat Sep 15 12:34:45 EDT 2007


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).










More information about the Python-list mailing list