diff options
author | Skip Montanaro <skip@pobox.com> | 2008-08-09 19:44:22 (GMT) |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2008-08-09 19:44:22 (GMT) |
commit | af8fcfaef58ee93fd20cfc9e20574f904a7ef802 (patch) | |
tree | 76564b238e0fe27c7d48461ced47d4bacf0c388a /Lib/csv.py | |
parent | bea655e53bac02bb873b2957332f8ff5ad8fc8e5 (diff) | |
download | cpython-af8fcfaef58ee93fd20cfc9e20574f904a7ef802.zip cpython-af8fcfaef58ee93fd20cfc9e20574f904a7ef802.tar.gz cpython-af8fcfaef58ee93fd20cfc9e20574f904a7ef802.tar.bz2 |
Merged revisions 65605 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r65605 | skip.montanaro | 2008-08-08 17:52:51 -0500 (Fri, 08 Aug 2008) | 1 line
accept issue 3436
........
Diffstat (limited to 'Lib/csv.py')
-rw-r--r-- | Lib/csv.py | 22 |
1 files changed, 18 insertions, 4 deletions
@@ -68,7 +68,7 @@ register_dialect("excel-tab", excel_tab) 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 + self._fieldnames = fieldnames # list of keys for the dict self.restkey = restkey # key to catch long rows self.restval = restval # default value for short rows self.reader = reader(f, dialect, *args, **kwds) @@ -78,11 +78,25 @@ class DictReader: def __iter__(self): return self + @property + def fieldnames(self): + if self._fieldnames is None: + try: + self._fieldnames = next(self.reader) + except StopIteration: + pass + self.line_num = self.reader.line_num + return self._fieldnames + + @fieldnames.setter + def fieldnames(self, value): + self._fieldnames = value + def __next__(self): + if self.line_num == 0: + # Used only for its side effect. + self.fieldnames row = next(self.reader) - if self.fieldnames is None: - self.fieldnames = row - row = next(self.reader) self.line_num = self.reader.line_num # unlike the basic reader, we prefer not to return blanks, |