diff options
author | Skip Montanaro <skip@pobox.com> | 2003-10-03 14:03:01 +0000 |
---|---|---|
committer | Skip Montanaro <skip@pobox.com> | 2003-10-03 14:03:01 +0000 |
commit | dffeed3ffa745d79a4a44a03c31895f6a63a5a79 (patch) | |
tree | 5050d559aaf33b03e6428654f25c8d1f1a74a9cc /Lib/csv.py | |
parent | Patch #813445: Add IPPROTO_IPV6 option to the socketmodule. (diff) | |
download | cpython-dffeed3ffa745d79a4a44a03c31895f6a63a5a79.tar.gz cpython-dffeed3ffa745d79a4a44a03c31895f6a63a5a79.tar.bz2 cpython-dffeed3ffa745d79a4a44a03c31895f6a63a5a79.zip |
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.
Diffstat (limited to 'Lib/csv.py')
-rw-r--r-- | Lib/csv.py | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/Lib/csv.py b/Lib/csv.py index 096badc7bbc..f2389fd3042 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -92,7 +92,7 @@ register_dialect("excel-tab", excel_tab) class DictReader: - def __init__(self, f, fieldnames, restkey=None, restval=None, + def __init__(self, f, fieldnames=None, restkey=None, restval=None, dialect="excel", *args, **kwds): self.fieldnames = fieldnames # list of keys for the dict self.restkey = restkey # key to catch long rows @@ -104,6 +104,10 @@ class DictReader: 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 # values |