lists + string

Steve Holden sholden at holdenweb.com
Fri May 10 13:24:10 EDT 2002


"ian" <iwaters at btclick.com> wrote in message
news:unSC8.57436$oK4.103913 at NewsReader...
> cheers for the quick response
> what im doing is reading a line from a file
> then appending it to my wordlist list.
> how would i do it knowing this???
>
> word = str(raw_input("Enter word to remove: "))
>
> wordlist = []
>     spamfile = file('spam.txt','r',1024)
>
Generally speaking you can do without the size hints, so

    spamfile = file("spam.txt", "r")

would do here (and you can even miss off the "r" at a pinch).

>     while 1:
>      line = spamfile.readline(1024)
>      if line == '':break
>      wordlist.append(line)
>     spamfile.close()
>
Watch that indentation! The difference bettwen 5 leading spaces and 6 is
obvious to the interpreter, but it makes itr difficult for old crocks like
me. 4 spaces per indentation level is nowadays considered acceptable.

So, you're building a list of words from the file. Note that these words
have newlines at the end of them because they have been read from a file...
you can strip these off by saying something like

    line = spamfile.readline().rstrip()

The readline() file method returns a string, which therefore has an rstrip()
method to remove trailing whitespace.

If you're happy about the newlines then you could just say

    wordlist = spamfile.readlines()

and pull the whole thing in with a single statement!

>     #write back file
>     #spamfile = file('spam.txt','w+',1024)
>     print "word list"
>     for x in wordlist:

Of course here you aren't taking the newlines into account. Because you
didn't open the file in binary mode (sensible decision) Python guarantees
that the line terminator is a line feed ('\n').

So probably the easiest thing would have been to add a newline to the word
you read in from the user.

>      print x
>      if x == word:print 'found'
>      #spamfile.write(x)
>     #spamfile.close()
>
[...]
So your program would become something like:

word = str(raw_input("Enter word to remove: "))+"\n"
spamfile = file('spam.txt','r',1024)
wordlist = =spamfile.readlines()
spamfile.close()

#write back file
spamfile = file('spam.txt','w+',1024)
print "word list"
for x in wordlist:
     print x
     if x == word:
        print 'found and removed'
     else:
        spamfile.write(x)
spamfile.close()

Hope this will be an iluminating contrast with your initial effort. You had
a lot ofthe logic correct, but not knowingf the details of the I/O subsystem
stood in your way. It was also a gaff to miss out the "else" in the
conditional: without it, everything gets written back to the spam file. In
that case, there's no point rewriting it, you could have just left it as it
was.

Tell your professor I'd like at least a "B" for this, please ;-)

regards
 Steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list