[Tutor] Reading text until a certain point

Alan Gauld alan.gauld at btinternet.com
Sat Jul 25 00:42:10 CEST 2009


"Alan Gauld" <alan.gauld at btinternet.com> wrote

> class Item(object):
>    def __init__(self, aFile):
>        data = aFile.readline().strip().split(:)
>        setattr(self, data[0], data[1])

The last two lines should of course be repeated 3 times...
Either in a loop or - for just 3 items - maybe hard coded...

> items = []
> f = open('myfile.txt')
> while f:  # stop when file is empty
>     items.append( Item(f) )     # process 3 lines

I also should have added that you need some exception 
handling in case the dta doesn't fit cleanly... but I did say 
it was pseudo code :-)

Alan G



More information about the Tutor mailing list