Proper way to reset csv.reader for multiple iterations?

后端 未结 3 2222
情话喂你
情话喂你 2021-02-20 07:47

Having an issue with a custom iterator in that it will only iterate over the file once. I am calling seek(0) on the relevant file object in between iterations, but

3条回答
  •  生来不讨喜
    2021-02-20 08:39

    The DictReader object does not appear to follow a seek() command on the open file, so the next() calls are continually made from the end of the file.

    In your reset you could reopen the file (you will also need to store the filename in self._filename):

    def reset(self):
         if self._file:
             self._file.close()
             self._file = open(self._filename, 'rb')
    

    You could also look at subclassing your file object similarly to the top answer to this question.

提交回复
热议问题