[Tutor] FW: wierd replace problem

Francesco Loffredo fal at libero.it
Tue Sep 14 12:56:06 CEST 2010


On 13/09/2010 20.21, Roelof Wobben wrote:
>...
>> The problem as stated in the book is :
>
> 3.Write a program called alice_words.py that creates a text file named alice_words.txt containing an alphabetical listing of all the words found in alice_in_wonderland.txt together with the number of times each word occurs. The first 10 lines of your output file should look something like this:
> Word Count
> =======================
> a 631
> a-piece 1
> abide 1
> able 1
> about 94
> above 3
> absence 1
> absurd 2How many times does the word, alice, occur in the book?
>
> The text can be found here : http://openbookproject.net/thinkcs/python/english2e/resources/ch10/alice_in_wonderland.txt
>
> So I open the file.
> Read the first rule.
>
> This is no problem for me.
>
> Then I want to remove some characters like ' , " when the word in the text begins with these characters.
> And there is the problem. The ' and " can't be removed with replace.
Not true. Your problem with replace() and strip() lies in the way you 
gave them the characters to remove, not in the file nor in the functions 
themselves. For example, you said:
> Strip ('"'') does not work.
> Still this message : SyntaxError: EOL while scanning string literal
Let's take a look into your argument:
(  parens to start the argument
'  first quote begins a string literal;
"  first character to remove, you want to delete all double quotes. OK.
'  ... what is this? Is it the end of the string, or is it another 
character to remove?
' This solves the doubt: '' means that the first string is over, and 
another has started. OK. (maybe not what you expected...)
)  OUCH! The argument has finished, but the string has not! So, End Of 
Line has been reached while your argument string was still open. Error.

The correct syntax (and you know where to look to read it!) should be:
MyText.strip('"\'')
The backslash tells Python that you want to include the following single 
quote in your string without terminating it, then the next single quote 
gives a proper end to the string itself.

Earlier, you said:
> I have tried that but then I see this message :
>
>  File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 8
>     letter2 = letter.strip('`")
>                               ^
> SyntaxError: EOL while scanning string literal
>
> Change it to (''`"") do not help either.
Sure: as some pointed out before, those arguments are NOT proper string 
literals, as the first one doesn't start and end with the SAME quoting 
character, while the second is made from two empty strings with a 
floating backquote ` between them.
If you want to strip (or replace) the three quoting characters
' " and ` , you should enclose them in a properly formatted string 
literal, that is one that starts and ends with the same quoting 
character and that, if must contain the same character, has it quoted by 
backslash:
.strip("'\"`")

> Am i now clear what the problem is Im facing.
>
> Roelof

I hope so. After having cut away all unwanted characters, you still have 
to face the main request from your tutorial, and THE question:(Sorry, 
Hamlet!) How many times does the word "alice" occur in the book?

I would guess... none!

Keep up your "fishing"
Francesco
-------------- next part --------------

Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com 
Versione: 9.0.851 / Database dei virus: 271.1.1/3132 -  Data di rilascio: 09/13/10 08:35:00


More information about the Tutor mailing list