Change newline character .readline() seeks

后端 未结 1 822
清歌不尽
清歌不尽 2020-12-19 09:41

Is it possible to change the newline character the .readline() method looks for while reading lines? I might have the need to read a stream from a file object

1条回答
  •  一向
    一向 (楼主)
    2020-12-19 10:11

    No.

    Consider creating a generator using file.read() and yielding chunks delimited by given character.

    Edit:

    The sample you provided should work just fine. I would prefer to use a generator though:

    def chunks(file, delim='\n'):
        buf = bytearray(), 
        while True:
            c = self.read(1)
            if c == '': return
            buf += c
            if c == delim: 
                yield str(buf)
                buf = bytearray()
    

    0 讨论(0)
提交回复
热议问题