Daniel Lenski added the comment: Here's the class I have been using for reading namedtuples from CSV files:
from collections import namedtuple from itertools import imap import csv class CsvNamedTupleReader(object): __slots__ = ('_r', 'row', 'fieldnames') def __init__(self, *args, **kwargs): self._r = csv.reader(*args, **kwargs) self.row = namedtuple("row", self._r.next()) self.fieldnames = self.row._fields def __iter__(self): #FIXME: how about this? return imap(self.row._make, self._r[:len(self.fieldnames)] return imap(self.row._make, self._r) dialect = property(lambda self: self._r.dialect) line_num = property(lambda self: self._r.line_num) This class wraps csv.reader since it doesn't seem to be possible to inherit from it. It uses itertools.imap to iterate over the rows output by csv.reader and convert them to the namedtuple class. One thing that needs fixing (marked with FIXME above) is what to do in the case of a row which has more fields than the header row. The simplest solution is simply to truncate such a row, but perhaps more options are needed, similar to those offered by DictReader. ---------- nosy: +dlenski _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue1818> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com