diff --git a/Lib/csv.py b/Lib/csv.py index fcb8a8fedce..096badc7bbc 100644 --- a/Lib/csv.py +++ b/Lib/csv.py @@ -93,11 +93,11 @@ register_dialect("excel-tab", excel_tab) class DictReader: def __init__(self, f, fieldnames, restkey=None, restval=None, - dialect="excel", *args): + dialect="excel", *args, **kwds): 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) + self.reader = reader(f, dialect, *args, **kwds) def __iter__(self): return self @@ -122,7 +122,7 @@ class DictReader: class DictWriter: def __init__(self, f, fieldnames, restval="", extrasaction="raise", - dialect="excel", *args): + dialect="excel", *args, **kwds): self.fieldnames = fieldnames # list of keys for the dict self.restval = restval # for writing short dicts if extrasaction.lower() not in ("raise", "ignore"): @@ -130,7 +130,7 @@ class DictWriter: ("extrasaction (%s) must be 'raise' or 'ignore'" % extrasaction) self.extrasaction = extrasaction - self.writer = writer(f, dialect, *args) + self.writer = writer(f, dialect, *args, **kwds) def _dict_to_list(self, rowdict): if self.extrasaction == "raise": diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py index ba7cc091b1c..d85c5b6cb39 100644 --- a/Lib/test/test_csv.py +++ b/Lib/test/test_csv.py @@ -447,6 +447,13 @@ class TestDictFields(unittest.TestCase): self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', "4": '4', "5": '5', "6": '6'}) + def test_read_semi_sep(self): + reader = csv.DictReader(["1;2;abc;4;5;6\r\n"], + fieldnames="1 2 3 4 5 6".split(), + delimiter=';') + self.assertEqual(reader.next(), {"1": '1', "2": '2', "3": 'abc', + "4": '4', "5": '5', "6": '6'}) + class TestArrayWrites(unittest.TestCase): def test_int_write(self): import array diff --git a/Misc/NEWS b/Misc/NEWS index b016161a7c8..a3b3a6c1e54 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -33,6 +33,9 @@ Extension modules Library ------- +- The csv module's DictReader and DictWriter classes now accept keyword + arguments. This was an omission in the initial implementation. + - The email package handles some RFC 2231 parameters with missing CHARSET fields better. It also includes a patch to parameter parsing when semicolons appear inside quotes.