If your file is small enough for you to read all of this in memory, you can use split:
for line in f.read().split('\0'): print line
Otherwise, you can try this recipe from a discussion of this function request :
def fileLineIter(inputFile, inputNewline="\n", outputNewline=None, 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')". """ if outputNewline is None: outputNewline = inputNewline partialLine = '' while True: charsJustRead = inputFile.read(readSize) if not charsJustRead: break partialLine += charsJustRead lines = partialLine.split(inputNewline) partialLine = lines.pop() for line in lines: yield line + outputNewline if partialLine: yield partialLine
I also noticed that your file has the extension "csv". Python has a built-in CSV module (import csv). There is the Dialect.lineterminator attribute, however, it is not currently implemented in the reader:
Dialect.lineterminator
The string used to complete the lines created by the author. The default is '\ r \ n'.
Note. The reader is hard-coded to recognize "\ r" or "\ n" as the end of a line and ignores the liner-determinant. This may change in the future.
Mark byers
source share