summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_csv.py
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2003-10-03 14:03:01 (GMT)
committerSkip Montanaro <skip@pobox.com>2003-10-03 14:03:01 (GMT)
commitdffeed3ffa745d79a4a44a03c31895f6a63a5a79 (patch)
tree5050d559aaf33b03e6428654f25c8d1f1a74a9cc /Lib/test/test_csv.py
parent3bbd6543a0c27fbeb1c1820d482dd6b89ebafaa7 (diff)
downloadcpython-dffeed3ffa745d79a4a44a03c31895f6a63a5a79.zip
cpython-dffeed3ffa745d79a4a44a03c31895f6a63a5a79.tar.gz
cpython-dffeed3ffa745d79a4a44a03c31895f6a63a5a79.tar.bz2
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/test/test_csv.py')
-rw-r--r--Lib/test/test_csv.py11
1 files changed, 10 insertions, 1 deletions
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(),