[Tutor] FW: wierd replace problem

bob gailer bgailer at gmail.com
Tue Sep 14 00:05:33 CEST 2010

  On 9/13/2010 2:21 PM, Roelof Wobben wrote:
> ----------------------------------------
>> From: rwobben at hotmail.com
>> To: bgailer at gmail.com
>> Subject: RE: [Tutor] wierd replace problem
>> Date: Mon, 13 Sep 2010 18:19:43 +0000
>>> I suggest you give a clear, complete and correct problem statement.
>>> Right now we are shooting in the dark at a moving target.
>>> Something like.
>>> Given the file alice_in_wonderland.txt, copied from url so-and-so
>>> Remove these characters ...
>>> Split into words (not letters?) where word is defined as
>>> Count the frequency of each word. =
>> Hello,
>> 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 2
> How many times does the word, alice, occur in the book?

We still do not have a definition of "word". Only some examples.

> 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. replace() will replace any character. You wrote in your other post

>  letter2 = letter.strip('`")

>  SyntaxError: EOL while scanning string literal

>  Change it to (''`"") do not help either.

Do you understand the error? strip expects a string.
'`" and ''`"" are NOT strings. Please review Python syntax for string literals.
Here again we bump into a fundamental problem - your not understanding some of the basics of Python.

> So in the output you will see something like this "dark instead of dark
> word is the words of the sentence which is read in from the text-file.
> Am i now clear what the problem is Im facing.
Somewhat clearer. We need a definition of "word". Examples help but are 
not definitions.

Example - word is a string of characters including a-z and -. The first 
and last characters must be in a-z. Your definition may be different.

BTW see http://dictionary.reference.com/browse/%27tis where 'tis IS a word.

Your original program (what DID become of the backslash???) is WAY off 
the mark. You must process one character at a time, decide whether it is 
the beginning of a word, the end of a word, within a word, or outside 
any word.

Take the beginning of the alice file, and BY HAND decide which category 
the first character is in. Then the 2nd. etc. That gives you the 
algorithm, Then translate that to Python.

Keep fishing. One day the struggle will be over.


Bob Gailer
Chapel Hill NC

More information about the Tutor mailing list