newbie question

Gerhard Häring gh_pythonlist at gmx.de
Fri Nov 2 04:03:40 EST 2001


On Fri, Nov 02, 2001 at 05:48:21AM +0000, The1Eagle wrote:
> Hello All,
> I am trying to read in a file and place each word in the file into an
> array or list so that those words can be manipulated (ie alphabetized,
> counted, deleted,
> compared, etc).  For some reason I can seem to find a way to get split
> to work in this manner.  Any ideas?  Here is the code so far, but this
> may be the wrong approach.  Thanks.
> 
> 
> #function that does all the work
> def handledata(input):
>     text = input.readlines()
>     #need to parse text.  Would be nice to have \n and other punctuation
> removed.

The readlines() string method returns a list. If you want to iterate
over all the lines, chances are you want something like:

for line in input.readlines():
    # ... do stuff with line here

To remove the trailing newline, you want something one of the the
strip() methods:

for line in input.readlines():
    line = line.strip():
    # continue doing stuff here

You can get very fancy with removing punctuation, but for the start I
recommend sticking to simple things like temporary variables and
for-loops. Later on the functional features of Python like filter, map,
apply, and so on are useful to write more compact code.

Next thing you want is the split() method of strings. It will return a
list again, have a look at the docs (it defaults to ' ' as delimiter).

For counting words, you should look into dictionaries.

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://www.cs.fhm.edu/~ifw00065/    OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))




More information about the Python-list mailing list