Canonical way of dealing with null-separated lines?
Christopher De Vries
devries at idolstarastronomer.com
Thu Feb 24 11:53:32 EST 2005
On Wed, Feb 23, 2005 at 10:54:50PM -0500, Douglas Alan wrote:
> Is there a canonical way of iterating over the lines of a file that
> are null-separated rather than newline-separated?
I'm not sure if there is a canonical method, but I would recommending using a
generator to get something like this, where 'f' is a file object:
def readnullsep(f):
# Need a place to put potential pieces of a null separated string
# across buffer boundaries
retain = []
while True:
instr = f.read(2048)
if len(instr)==0:
# End of file
break
# Split over nulls
splitstr = instr.split('\0')
# Combine with anything left over from previous read
retain.append(splitstr[0])
splitstr[0] = ''.join(retain)
# Keep last piece for next loop and yield the rest
retain = [splitstr[-1]]
for element in splitstr[:-1]:
yield element
# yield anything left over
yield retain[0]
Chris
More information about the Python-list
mailing list