Skip to content

Add named tuple reader to CSV module #46143

Closed

Description

@rhettinger
BPO 1818
Nosy @smontanaro, @warsaw, @rhettinger, @pitrou, @merwok, @cedk, @asvetlov, @dlenski
Files
  • ntreader.diff: Proof-of-concept patch
  • ntreader3.diff: namedtuple reader and writer.
  • ntreader4.diff: Includes revision for rename keyword argument
  • named_tuple_write_header2.patch
  • ntreader4_py3_1.diff: Patch against python 3.1a1
  • ntreader6_py3.diff: updated documentation
  • ntreader6_py27.diff: updated documentation
  • 1818_py35.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/smontanaro'
    closed_at = <Date 2020-12-22.02:45:54.546>
    created_at = <Date 2008-01-13.22:27:14.884>
    labels = ['3.8', 'type-feature', 'library']
    title = 'Add named tuple reader to CSV module'
    updated_at = <Date 2020-12-22.02:45:54.545>
    user = 'https://github.com/rhettinger'

    bugs.python.org fields:

    activity = <Date 2020-12-22.02:45:54.545>
    actor = 'rhettinger'
    assignee = 'skip.montanaro'
    closed = True
    closed_date = <Date 2020-12-22.02:45:54.546>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2008-01-13.22:27:14.884>
    creator = 'rhettinger'
    dependencies = []
    files = ['9151', '12990', '13009', '13188', '13263', '13274', '13275', '39139']
    hgrepos = []
    issue_num = 1818
    keywords = ['patch']
    message_count = 42.0
    messages = ['59866', '61523', '61532', '81453', '81464', '81518', '81537', '82744', '82745', '82746', '82764', '82765', '82770', '82771', '82778', '82780', '82798', '82799', '82812', '82814', '82819', '83298', '83299', '83310', '83318', '83321', '83332', '83333', '83334', '83340', '102936', '102959', '110598', '111523', '111552', '115348', '235710', '241599', '241601', '242879', '242893', '311182']
    nosy_count = 11.0
    nosy_names = ['skip.montanaro', 'barry', 'rhettinger', 'pitrou', 'eric.araujo', 'ced', 'jdwhitley', 'rrenaud', 'asvetlov', 'dlenski', 'copper-head']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue1818'
    versions = ['Python 3.8']

    Activity

    1. rhettinger commented on Jan 13, 2008

      @rhettinger
      ContributorAuthor

      Here's a proof-of-concept patch. If approved, will change from
      generator form to match the other readers and will add a test suite.

      The idea corresponds to what is currently done by the dict reader but
      returns a space and time efficient named tuple instead of a dict. Field
      order is preserved and named attribute access is supported.

      A writer is not needed because named tuples can be feed into the
      existing writer just like regular tuples.

    2. added
      stdlib
      on Jan 13, 2008
    3. rhettinger commented on Jan 22, 2008

      @rhettinger
      ContributorAuthor

      Barry, any thoughts on this?

    4. smontanaro commented on Jan 22, 2008

      @smontanaro
      Contributor

      I'd personally be kind of surprised if Barry had any thoughts on this.
      Is there any reason this couldn't be pushed down into the C code and
      replace the normal tuple output completely? In the absence of any
      fieldnames you could just dream some up, like "field001", "field002",
      etc.

      Skip

    5. jdwhitley commented on Feb 9, 2009

      jdwhitleymannequin
      Mannequin

      An implementation of a namedtuple reader and writer.

      Created a writer for the case where user would like to specify
      desired field names and default values on missing field names.

      e.g.
      mywriter = NamedTupleWriter(f, fieldnames=['f1', 'f2', 'f3'],
      restval='missing')

      Nt = namedtuple('LessFields', 'f1 f3')
      nt = Nt(f1='one', f2=2)
      
      mywriter.writerow(nt) # writes one,missing,2

      any thoughts on case where defined fieldname has a leading
      underscore? Should there be a flag to silently ignore?

      e.g.
      if self._ignore_underscores:
      fieldname = fieldname.lstrip('_')

      Leading underscores may be present in an unsighted csv file,
      additionally, spaces and other non alpha numeric characters pose
      a problem that does not affect the DictReader class.

      Cheers,

    6. rhettinger commented on Feb 9, 2009

      @rhettinger
      ContributorAuthor

      Consider providing a hook to a function that converts non-conforming
      field names (ones with a leading underscore, leading digit, non-letter,
      keyword, or duplicate name).

      class NamedTupleReader:
          def __init__(self, f, fieldnames=None, restkey=None, restval=None,
                       dialect="excel", fieldnamer=None, *args, **kwds):
                       . . .

      I'm going to either post a recipe to do the renaming or provide a static
      method for the same purpose. It might work like this:

        >>> renamer(['abc', 'def', '1', '_hidden', 'abc', 'p', 'abc'])
        ['abc', 'x_def', 'x_1', 'x_hidden', 'x_abc', 'p', 'x1_abc']
    7. rhettinger commented on Feb 10, 2009

      @rhettinger
      ContributorAuthor

      In r69480, named tuples gained the ability to automatically rename
      invalid fieldnames.

    8. jdwhitley commented on Feb 10, 2009

      jdwhitleymannequin
      Mannequin

      Updated NamedTupleReader to give a rename=False keyword argument.
      rename is passed directly to the namedtuple factory function to enable
      automatic handling of invalid fieldnames.

      Two new tests for the rename keyword.

      Cheers,

    9. rrenaud commented on Feb 26, 2009

      rrenaudmannequin
      Mannequin

      I am totally new to Python dev. I reinvented a NamedTupleReader
      tonight, only to find out that it was created a year ago. My primary
      motivation is that DictReader reads headers nicely, but DictWriter
      totally sucks at handling them.

      Consider doing some filtering on a csv file, like so.

      sample_data = [
          'title,latitude,longitude',
          'OHO Ofner & Hammecke Reinigungsgesellschaft mbH,48.128265,11.610848',
          'Kitchen Kaboodle,45.544241,-122.715728',
          'Walgreens,28.339727,-81.596367',
          'Gurnigel Pass,46.731944,7.447778'
          ]
      
      def filter_with_dict_reader_writer():
        accepted_rows = []
        for row in csv.DictReader(sample_data):
          if float(row['latitude']) > 0.0 and float(row['longitude']) > 0.0:
            accepted_rows.append(row)
      
        field_names = csv.reader(sample_data).next()
        output_writer = csv.DictWriter(open('accepted_by_dict.csv', 'w'),
                                       field_names)
        output_writer.writerow(dict(zip(field_names, field_names)))
        output_writer.writerows(accepted_rows)

      You have to work so hard to maintain the headers when you write the file
      with DictWriter. I understand this is a limitation of dicts throwing
      away the order information. But namedtuples don't have that problem.

      NamedTupleReader and NamedTupleWriter should be inverses. This means
      that NamedTupleWriter needs to write headers. This should produce
      identical output as the dict writer example, but it's much cleaner.

      def filter_with_named_tuple_reader_writer():
         accepted_rows = []
         for row in csv.NamedTupleReader(sample_data):
           if float(row.latitude) > 0.0 and float(row.longitude) > 0.0:
             accepted_rows.append(row)
      
         output_writer = csv.NamedTupleWriter(
             open('accepted_by_named_tuple.csv', 'w'))
         output_writer.writerows(accepted_rows)

      I patched on top of the existing NamedTupleWriter patch adding support
      for writing headers. I don't know if that's bad style/etiquette, etc.

    10. rrenaud commented on Feb 26, 2009

      rrenaudmannequin
      Mannequin

      My previous patch could write the header twice. But I am not sure about
      about how the writer should handle the fieldnames parameter on one hand,
      and the namedtuple._fields on the other.

    11. rhettinger commented on Feb 26, 2009

      @rhettinger
      ContributorAuthor

      The two latest patches (ntreader4.diff and
      named_tuple_write_header.patch) seem like they are going in the right
      direction and are getting close.

      Barry or Skip, is this something you want in your module?

    12. added
      type-feature
      on Feb 26, 2009
    13. smontanaro commented on Feb 26, 2009

      @smontanaro
      Contributor

      Raymond> Barry or Skip, is this something you want in your module?

      Sorry, I haven't really looked at this ticket other than to notice its
      presence. I wrote the DictReader/DictWriter functions way back when, so I'm
      pretty comfortable using them. I haven't felt the need for any other reader
      or writer which manipulates file headers.

      Skip

    14. warsaw commented on Feb 26, 2009

      @warsaw
      Member

      I think it would be useful to have.

    15. 24 remaining items

    16. rhettinger commented on Sep 2, 2010

      @rhettinger
      ContributorAuthor

      Unassigning, this needs fresh thought and a fresh patch from someone who can devote a little deep thinking on how to solve this problem cleanly. In the meantime, it is no problem to simply cast the CSV tuples into named tuples.

    17. removed their assignment
      on Sep 2, 2010
    18. dlenski commented on Feb 10, 2015

      dlenskimannequin
      Mannequin

      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.

    19. copper-head commented on Apr 20, 2015

      copper-headmannequin
      Mannequin

      As my contribution during the sprints at PyCon 2015, I've tweaked Jervis's patch a little and updated the tests/docs to work with Python 3.5.

      My only real change was placing the basic reader object inside a generator expression that filters out empty lines. Being partial to functional programming I find this removes some of the code clutter in __next__(), letting that method focus on turning rows into tuples.

      Hopefully this will rekindle the discussion!

    20. rhettinger commented on Apr 20, 2015

      @rhettinger
      ContributorAuthor

      Skip or Barry, do you want to look at this?

    21. copper-head commented on May 11, 2015

      copper-headmannequin
      Mannequin

      Friendly reminder that this exists.

      I know everyone's busy and this is marked as low-priority, but I'm gonna keep bumping this till we add a solution :)

    22. smontanaro commented on May 11, 2015

      @smontanaro
      Contributor

      I looked at this six years ago. I still haven't found a situation where I pined for a NamedTupleReader. That said, I have no objection to committing it if others, more well-versed in current Python code and NamedTuples than I gives it a pass. Note that I added a couple comments to the csv.py diff, but nobody either updated the code or explained why I was out in the weeds in my comments.

    23. smontanaro commented on Jan 29, 2018

      @smontanaro
      Contributor

      FWIW, I relinquished my check-in privileges quite awhile ago. This should
      almost certainly no longer be assigned to me.

      S

    24. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    Labels

    3.8 (EOL)stdlibtype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions