[Tutor] wierd replace problem

Roelof Wobben rwobben at hotmail.com
Mon Sep 13 18:46:09 CEST 2010




----------------------------------------
> From: rwobben at hotmail.com
> To: joel.goldstick at gmail.com
> Subject: RE: [Tutor] wierd replace problem
> Date: Mon, 13 Sep 2010 16:45:28 +0000
>
>
>
>
> ________________________________
>> Date: Mon, 13 Sep 2010 12:42:56 -0400
>> From: joel.goldstick at gmail.com
>> To: tutor at python.org
>> Subject: Re: [Tutor] wierd replace problem
>>
>>
>>
>> On Mon, Sep 13, 2010 at 12:37 PM, Roelof Wobben
>>> wrote:
>>
>>
>>
>> ----------------------------------------
>> Date: Mon, 13 Sep 2010 12:17:47 -0400
>> From: michael at trollope.org
>> To: tutor at python.org
>> Subject: Re: [Tutor] wierd replace problem
>>
>>
>> On Mon, Sep 13, 2010 at 12:19:23PM +0000, Roelof Wobben wrote:
>>>
>>> Hello,
>>>
>>> I have this string called test with the contents of 'het is een
>> wonder \\TIS'
>>>
>>> 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.
>>>
>>> Now I try the same in a IDE with this programm :
>>>
>>> woorden =[]
>>> letter_counts = {}
>>> file = open ('alice_in_wonderland.txt', 'r')
>>> for line in file:
>>> line2 = line.replace ("\\","")
>>> line3 = line2.lower()
>>> woorden = line3.split()
>>> for letter in woorden:
>>> letter_counts[letter] = letter_counts.get (letter, 0) + 1
>>> letter_items = letter_counts.items()
>>> letter_items.sort()
>>> print letter_items
>>>
>>> But now Im gettting this output :
>>>
>>> [('"\'tis', 1),
>>>
>>> Why does the \ stays here. It should have gone as the test in the
>> python prompt says.
>>
>> Hello,
>>
>> Actually, on closer look I can see the answer.
>>
>> The original text must look something like this:
>>
>> \\"'tis the season to be jolly," said santa.
>>
>> When you run your process against a string in that format, you get the
>> output shown: ('"\'tis', 1). The appearance of the backslash is
>> fortuitous. It has nothing to do with the string.replace(), it's
>> there to escape the single quote, which is appearing in the middle of
>> a single-quoted string. IF the double-quote had not also been there,
>> python would have replaced the outer quotes with double quotes, as it
>> does on my system before I got to thinking about that lonely double
>> quote.
>>
>> Thanks.
>>
>> mp
>>
>> --
>> Michael Powe michael at trollope.org
>> Naugatuck CT USA
>> "I wrote what I did because as a woman, as a mother, I was oppressed
>> and brokenhearted with the sorrows and injustice I saw, because as a
>> Christian I felt the dishonor to Christianity, -- because as a lover
>> of my country, I trembled at the coming day of wrath." -- H.B. Stowe
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>> Hello Michael,
>>
>> The original text is this :
>>
>> `'Tis so,' said the Duchess: `and the moral of that is--"Oh,
>> 'tis love, 'tis love, that makes the world go round!"'
>>
>> So I think I have to replace the '.
>>
>> Roelof
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>> That is a completely different problem than the one you originally
>> posed. I doubt that what you inserted above is actually completely
>> correct. It opens with a back tick, has a back tick before and, then
>> ens with what looks like a double quote then a single quote
>> --
>> Joel Goldstick
>>
>>
>> _______________________________________________ Tutor maillist -
>> Tutor at python.org To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>

Hello Joel,

The orginal text can be found here : http://openbookproject.net/thinkcs/python/english2e/resources/ch10/alice_in_wonderland.txt
So you can see I copied it right.

Roelof 		 	   		  


More information about the Tutor mailing list