Message235710
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. |
|
Date |
User |
Action |
Args |
2015-02-10 21:41:01 | dlenski | set | recipients:
+ dlenski, barry, rhettinger, pitrou, eric.araujo, jdwhitley, rrenaud, asvetlov |
2015-02-10 21:41:01 | dlenski | set | messageid: <1423604461.47.0.376796699515.issue1818@psf.upfronthosting.co.za> |
2015-02-10 21:41:01 | dlenski | link | issue1818 messages |
2015-02-10 21:41:00 | dlenski | create | |
|