[issue1152248] Add support for reading records with arbitrary separators to the standard IO stack

Andrew Barnert report at bugs.python.org
Sun Jul 20 02:45:25 CEST 2014


Andrew Barnert added the comment:

One last thing, a quick & dirty solution that works today, if you don't mind accessing private internals of stdlib classes, and don't mind giving up the performance of _io for _pyio, and don't need a solution for binary files:

class MyTextIOWrapper(_pyio.TextIOWrapper):
    def readrecord(self, sep):
        readnl, self._readnl = self._readnl, sep
        try:
            return self.readline()
        finally:
            self._readnl = readnl

Or, if you prefer:

class MyTextIOWrapper(_pyio.TextIOWrapper):
    def __init__(self, *args, separator, **kwargs):
        super().__init__(*args, **kwargs)
        self._readnl = separator

For binary files, there's no solution quite as simple; you need to write your own readline method by copying and pasting the one from _pyio.RawIOBase, and the modifications to use an arbitrary separator aren't quite as trivial as they look at first (at least if you want multi-byte separators).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue1152248>
_______________________________________


More information about the Python-bugs-list mailing list