append special chars with "\"

Christos TZOTZIOY Georgiou tzot at sil-tec.gr
Tue Sep 30 12:34:54 EDT 2003


On Tue, 30 Sep 2003 14:50:19 GMT, rumours say that Alex Martelli
<aleax at aleax.it> might have written:

>Christos TZOTZIOY Georgiou wrote:
>
>> On Tue, 30 Sep 2003 07:58:27 GMT, rumours say that Alex Martelli
>> <aleax at aleax.it> might have written:
>> 
>>>charmap = {}
>>>for c in chr: charmap[c] = c+'\\'
>> 
>> I believe you meant charmap[c] = '\\' + c ...
>
>Sure, if the original poster wants the backslash BEFORE the character
>(the "append" in the subject suggested to me he wanted it AFTER).

You are correct about the subject (append instead of 'pre-pend' -sp?),
but the OP's code was not appending, in spite of the comments:

for i in str:
     if i in chr:          # if special character in str
         n+='\\'           # append it with a backslash
     n+=i

I just assumed the OP was a better coder than English speaker :)
-- 
TZOTZIOY, I speak England very best,
Ils sont fous ces Redmontains! --Harddix




More information about the Python-list mailing list