This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dlenski
Recipients asvetlov, barry, dlenski, eric.araujo, jdwhitley, pitrou, rhettinger, rrenaud
Date 2015-02-10.21:41:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
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.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.
Date User Action Args
2015-02-10 21:41:01dlenskisetrecipients: + dlenski, barry, rhettinger, pitrou, eric.araujo, jdwhitley, rrenaud, asvetlov
2015-02-10 21:41:01dlenskisetmessageid: <>
2015-02-10 21:41:01dlenskilinkissue1818 messages
2015-02-10 21:41:00dlenskicreate