input record seperator (equivalent of "$|" of perl)
M.E.Farmer
mefjr75 at hotmail.com
Sun Dec 19 19:17:13 EST 2004
What about a generator and xreadlines for those really large files:
py>def recordbreaker(recordpath, seperator='#'):
... rec = open(recordpath ,'r')
... xrecord = rec.xreadlines()
... a =[]
... for line in xrecord:
... sep = line.find(seperator)
... if sep != -1:
... a.append(line[:sep])
... out = ''.join(a)
... a =[]
... a.append(line[sep+1:])
... yield out
... else:
... a.append(line)
... if a:
... yield ''.join(a)
... rec.close()
...
py>records = recordbreaker('/tmp/myrecords.txt')
py>for item in records:
... print item
M.E.Farmer
les_ander at yahoo.com wrote:
> Hi,
> I know that i can do readline() from a file object.
> However, how can I read till a specific seperator?
> for exmple,
> if my files are
>
> name
> profession
> id
> #
> name2
> profession3
> id2
>
> I would like to read this file as a record.
> I can do this in perl by defining a record seperator;
> is there an equivalent in python?
> thanks
More information about the Python-list
mailing list