It's sometimes annoying that a csv.DictReader doesn't retain the field ordering given in the first line of the file. Sometimes it matters.
This patch converts the reader so that it returns an OrderedDict rather than a plain dict, thereby retaining the ordering.
All tests still pass (though I haven't yet added a test to verify that the field ordering *is* retained - didn't think it was worth it if the patch won't be added, but will happily add that test otherwise).
I have updated the documentation, but was unable in the time available to find out how to correctly reference the OrderedDict class so that it was correctly hyperlinked.
Testing could be interesting. I'm thinking of generating five random string keys with a couple of rows of data, creating csv StringIOs (using pure Python) for all 120 combinations and verifying that they read back in the order they were written.
We should also test that OrderedDicts write correctly with a DictWriter and the same key sets.
It's a fairly haphazard test plan, so I'll be happy to hear more thorough suggestions. We could at a pinch reduce the number of keys to three if speed considerations dictate.
OK, here's what I think should be close to the final patch. I've updated the documentation, rebuilt it and verified it reads OK, and confirmed that the new code passes all tests except those skipped for platform reasons (I think they expect a Windows environment). This includes the new test to confirm that ordering is retained over all 120 possible combinations of five keys.
Please let me know if any further updates are needed.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
The text was updated successfully, but these errors were encountered: