file reading by record separator (not line by line)

Hendrik van Rooyen mail at microcorp.co.za
Fri Jun 1 07:57:44 CEST 2007


 "Lee Sander" <le..e at gmail.com>wrote:


> I wanted to also say that this file is really huge, so I cannot
> just do a read() and then split on ">" to get a record
> thanks
> lee
> 
> On May 31, 1:26 pm, Lee Sander <lesa... at gmail.com> wrote:
> > Dear all,
> > I would like toreada really hugefilethat looks like this:
> >
> > > name1....
> >
> > line_11
> > line_12
> > line_13
> > ...>name2 ...
> >
> > line_21
> > line_22
> > ...
> > etc
> >
> > where line_ij is just a free form text on that line.
> >
> > how can ireadfileso that every time i do a "read()" i get exactly
> > onerecord
> > up to the next ">"
> >
> > many thanks
> > Lee
> 

I would do something like: (not tested):

def get_a_record(f,sep):
  ret_rec = ''
  while True:
    char = f.read(1)
    if char == sep:
      break
    else:
      ret_rec += char
  return ret_rec

- Hendrik




More information about the Python-list mailing list