[Tutor] wierd replace problem

Roelof Wobben rwobben at hotmail.com
Mon Sep 13 19:50:04 CEST 2010




----------------------------------------
> To: tutor at python.org
> From: alan.gauld at btinternet.com
> Date: Mon, 13 Sep 2010 18:28:46 +0100
> Subject: Re: [Tutor] wierd replace problem
>
>
> "Roelof Wobben" wrote
>
>> Now I want to get rid of the \\ so I do this : test2 = test.replace
>> ('\\', '')
>> And I get at the python prompt this answer : 'het is een wonder TIS'
>> So that's right.
>
> OK,. Thats replacing a double slash in the data
>
>> for line in file:
>> line2 = line.replace ("\\","")
>
> And this is doing the same. Any double slashes in your file content
> will be replaced.
>
>> letter_items = letter_counts.items()
>> letter_items.sort()
>> print letter_items
>
> Now we have an issue of representing characters.
>
>> [('"\'tis', 1),
>
> This is a representation issue. Python is using the \ to escape
> the single quote since you are using single quotes on the outside.
> Without the backslash the \' would look like the end of the
> string to Python.
>
> If you print the actual string it will not show the quote:
>
> for item in letter_items:
> print item[0]
>
> Backslashes are awkward characters because they are used
> for several different special purposes as well as being characters
> in their own right. If you had tried replacing almost any other
> character you would not have gotten confused.
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
>>
>> Why does the \ stays here. It should have gone as the test in the
>> python prompt says.
>>
>> Roelof
>>
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
 
hello Alan, 
 
Your right. Then it prints like this "'tis
Which is not right. It must be tis.
So the replace does not what it supposed to do.
 
Roelof

  		 	   		  


More information about the Tutor mailing list