summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/csv.py6
-rw-r--r--Lib/test/test_csv.py11
2 files changed, 15 insertions, 2 deletions
diff --git a/Lib/csv.py b/Lib/csv.py
index 096badc..f2389fd 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
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index d85c5b6..29a13cb 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -382,7 +382,6 @@ class TestQuotedEscapedExcel(TestCsvBase):
def test_read_escape_fieldsep(self):
self.readerAssertEqual('"abc\\,def"\r\n', [['abc,def']])
-# Disabled, pending support in csv.utils module
class TestDictFields(unittest.TestCase):
### "long" means the row is longer than the number of fieldnames
### "short" means there are fewer elements in the row than fieldnames
@@ -401,6 +400,10 @@ class TestDictFields(unittest.TestCase):
fieldnames=["f1", "f2", "f3"])
self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'})
+ def test_read_dict_no_fieldnames(self):
+ reader = csv.DictReader(StringIO("f1,f2,f3\r\n1,2,abc\r\n"))
+ self.assertEqual(reader.next(), {"f1": '1', "f2": '2', "f3": 'abc'})
+
def test_read_long(self):
reader = csv.DictReader(StringIO("1,2,abc,4,5,6\r\n"),
fieldnames=["f1", "f2"])
@@ -413,6 +416,12 @@ class TestDictFields(unittest.TestCase):
self.assertEqual(reader.next(), {"f1": '1', "f2": '2',
"_rest": ["abc", "4", "5", "6"]})
+ def test_read_long_with_rest_no_fieldnames(self):
+ reader = csv.DictReader(StringIO("f1,f2\r\n1,2,abc,4,5,6\r\n"),
+ restkey="_rest")
+ self.assertEqual(reader.next(), {"f1": '1', "f2": '2',
+ "_rest": ["abc", "4", "5", "6"]})
+
def test_read_short(self):
reader = csv.DictReader(["1,2,abc,4,5,6\r\n","1,2,abc\r\n"],
fieldnames="1 2 3 4 5 6".split(),