[Tutor] Reading elements in a file

Jeff Peery jeffpeery at seametrics.com
Wed May 4 21:34:34 CEST 2011


Greg,
You might try…

Import sting
delimiter = ‘.’
f = open('words.txt', "r")
lines = f.readlines()
for line in lines:
            line_items = string.split(line,delimiter)


From: tutor-bounces+jeffpeery=seametrics.com at python.org [mailto:tutor-bounces+jeffpeery=seametrics.com at python.org] On Behalf Of Greg Christian
Sent: Wednesday, May 04, 2011 11:38 AM
To: tutor at python.org
Subject: [Tutor] Reading elements in a file

I am kind of stuck on what is probably a simple thing:

If we have a file of words like this: “first”,”word”,”in”,”that”,”another”,”part”,”of”,”this”

f = open('words.txt', "r")
    words = f.read()

will read the whole file, is there a way to read just the words: first word in that another part of this

I guess I have to separate on the “,” but I am not quite sure how to go about this.

Any input would be appreciated.

Regards,

Greg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110504/9cc3ce59/attachment-0001.html>


More information about the Tutor mailing list