Canonical way of dealing with null-separated lines?
nessus at mit.edu
Tue Mar 1 02:11:55 CET 2005
> Oops, I just realized that my previously definitive version did not
> handle multi-character newlines. So here is a new definitive
> version. Oog, now my brain hurts:
I dunno what I was thinking. That version sucked! Here's a version
that's actually comprehensible, a fraction of the size, and works in
all cases. (I think.)
def fileLineIter(inputFile, newline='\n', leaveNewline=False, readSize=8192):
"""Like the normal file iter but you can set what string indicates newline.
The newline string can be arbitrarily long; it need not be restricted to a
single character. You can also set the read size and control whether or not
the newline string is left on the end of the iterated lines. Setting
newline to '\0' is particularly good for use with an input file created with
something like "os.popen('find -print0')".
outputLineEnd = ("", newline)[leaveNewline]
partialLine = ''
charsJustRead = inputFile.read(readSize)
if not charsJustRead: break
lines = (partialLine + charsJustRead).split(newline)
partialLine = lines.pop()
for line in lines: yield line + outputLineEnd
if partialLine: yield partialLine
More information about the Python-list