Read a record instead of a line from a file

Simon Brunning SBrunning at trisystems.co.uk
Fri Aug 24 10:40:12 EDT 2001


> From:	YMK [SMTP:gbeymk at sgh.com.sg]
> If I know the "Record Separator" of a flat file, how do I set to read
> one record at a time ?
 
If the file isn't large, I would suggest that you just read the whole lot
into a string, and split it like so (untested):

myfile = open('whatever')
myfiledata = myfile.read()
myfilelines = myfiledata.split(mydelimiter)

If the file is large, then you are going to have to do something clever. 

(Hmmm, sounds like *exactly* the sort of thing that Python 2.2's generators
were designed for! I can't play now, 'cos I'm at work, but I'll try
something this evening. Of course, the Martellibot will beat me too it,
*and* come up with a more elegant solution...)

Cheers,
Simon Brunning
TriSystems Ltd.
sbrunning at trisystems.co.uk




-----------------------------------------------------------------------
The information in this email is confidential and may be legally privileged.
It is intended solely for the addressee. Access to this email by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution, or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
accept liability for statements made which are clearly the senders own.




More information about the Python-list mailing list