[Python-checkins] python/dist/src/Lib csv.py,1.9,1.10

montanaro at users.sourceforge.net montanaro at users.sourceforge.net
Fri Oct 3 10:03:03 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv20930/Lib

Modified Files:
	csv.py 
Log Message:
Make the fieldnames argument optional in the DictReader.  If self.fieldnames
is None, the next row read is used as the fieldnames.  In the common case,
this means the programmer doesn't need to know the fieldnames ahead of time.
The first row of the file will be used.  In the uncommon case, this means
the programmer can set the reader's fieldnames attribute to None at any time
and have the next row read as the next set of fieldnames, so a csv file can
contain several "sections", each with different fieldnames.



Index: csv.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/csv.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** csv.py	6 Sep 2003 19:52:12 -0000	1.9
--- csv.py	3 Oct 2003 14:03:01 -0000	1.10
***************
*** 93,97 ****
  
  class DictReader:
!     def __init__(self, f, fieldnames, restkey=None, restval=None,
                   dialect="excel", *args, **kwds):
          self.fieldnames = fieldnames    # list of keys for the dict
--- 93,97 ----
  
  class DictReader:
!     def __init__(self, f, fieldnames=None, restkey=None, restval=None,
                   dialect="excel", *args, **kwds):
          self.fieldnames = fieldnames    # list of keys for the dict
***************
*** 105,108 ****
--- 105,112 ----
      def next(self):
          row = self.reader.next()
+         if self.fieldnames is None:
+             self.fieldnames = row
+             row = self.reader.next()
+ 
          # unlike the basic reader, we prefer not to return blanks,
          # because we will typically wind up with a dict full of None





More information about the Python-checkins mailing list