0

I have file /my/f, which I open as:

with open('/my/f') as data_file:
    for line in data_file:
        print(line)

I do not want to read the whole file at once and I would like for the implicit iterator over the file lines to consider ASCII's NUL (0x00, $'\0', etc.) as a line-separator.

Robottinosino
  • 10,384
  • 17
  • 59
  • 97
  • 1
    You could try `split`ing the string by `chr(0)` and iterating over that instead. – Jack TC May 23 '13 at 11:15
  • 1
    Side note: The correct word would be null-terminated *record*. A line, by definition, is a record ending with a newline character and makes sense only in text files. – Bakuriu May 23 '13 at 11:34
  • Agree, record is a better word. Disagree with "null", ASCII says it's NUL. "null" is overloaded. – Robottinosino May 23 '13 at 11:40

1 Answers1

1

Another way than the extensive solution in the suggested duplicate is simply to subclass the File object's iterator, to iterate over NUL instead.

class NulFile(file):
  buf=''
  bufsize=1024
  def __iter__(self):
    return self
  def next(self):
    while True:
      n = self.buf.find('\0')
      if n == -1:
        s = self.read(self.bufsize)
        if len(self.buf) == 0 and len(s) == 0:
          raise StopIteration
        elif len(s) == 0:
          break
        self.buf += s
      else:
        res = self.buf[:n]
        self.buf = self.buf[n+1:]
        return res
    res = self.buf
    self.buf = ''
    return res

But, instead of opening the file with open(..), you use NulFile(..).

MrGumble
  • 5,631
  • 1
  • 18
  • 33