-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
csv module: add header row to DictWriter #43803
Comments
I use the DictWriter class from the csv module, and def writeheader(self, headernames = {}):
"""Write a header row"""
if not headernames:
headernames = dict(zip(self.fieldnames,
self.fieldnames))
self.writerow(headernames) This would let you either use the fieldnames directly, Would be nice to have another keyword argument to At the moment I have to write things like fields = ['a','b','c']
w = csv.DictWriter(fid, fields)
w.writerow(dict(zip(fields, fields)))
for row in rows:
w.writerow(row) The proposed changes would let me write the simpler w = csv.DictWriter(fid, ['a','b','c'], header = True)
for row in rows:
w.writerow(row) A problem is that including a new keyword argument |
I don't see a patch. Is there some reason that if you need this |
Skip, you were arguing in another csv issue on a NamedTupleReader that Certainly, making this default functionality for DictWriter would A sample process of reading and writing might be:
My feeling is, if DictReader can read in head names, why can't My feeling of how to implement this functionality would be to include a I certainly am miffed by the fact that DictWriter cannot produce |
I'd like to commit this, but it would be nice to get a review first: Index: Lib/csv.py --- Lib/csv.py (revision 76697)
+++ Lib/csv.py (working copy)
@@ -132,6 +132,10 @@
self.extrasaction = extrasaction
self.writer = writer(f, dialect, *args, **kwds)
+ def writeheader(self):
+ header = dict(zip(self.fieldnames, self.fieldnames))
+ self.writerow(header)
+
def _dict_to_list(self, rowdict):
if self.extrasaction == "raise":
wrong_fields = [k for k in rowdict if k not in self.fieldnames]
Index: Lib/test/test_csv.py
===================================================================
--- Lib/test/test_csv.py (revision 76697)
+++ Lib/test/test_csv.py (working copy)
@@ -598,8 +598,10 @@
fileobj = os.fdopen(fd, "w+b")
try:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2",
"f3"])
+ writer.writeheader()
writer.writerow({"f1": 10, "f3": "abc"})
fileobj.seek(0)
+ self.assertEqual(fileobj.readline(), "f1,f2,f3\r\n")
self.assertEqual(fileobj.read(), "10,,abc\r\n")
finally:
fileobj.close() (I think I have commit privileges already.) |
I'm sorry, but I don't have time to look at this right now. On the one S |
Skip, I agree that it's hard to decide if we should have the class write |
We can't change default behaviour because it will break compatibility, |
Antoine> We can't change default behaviour because it will break Why can't default behavior be changed? S |
Le lundi 07 décembre 2009 à 23:42 +0000, Skip Montanaro a écrit :
Well, because it will break assumptions about the generated documents? |
Antoine> We can't change default behaviour because it will break
Isn't the alpha period (2.7 and 3.2 in this case) precisely when an API can Skip |
Well, it can, but only if there are compelling reasons to do so. It |
Fine. Dirkjan, assuming there are the necessary test cases and Skip |
Fixed in SVN, r78384. |
according to the buidbots, it hurts some platforms: Windows XP, Windows 7 and sparc Solaris10
test_writerows (test.test_csv.Test_Csv) ... ok ====================================================================== Traceback (most recent call last):
File "/home2/buildbot/slave/trunk.loewis-sun/build/Lib/test/test_csv.py", line 607, in test_write_simple_dict
self.assertEqual(fileobj.read(), "10,,abc\r\n")
AssertionError: 'f1,f2,f3\r\n' != '10,,abc\r\n' Ran 84 tests in 17.335s FAILED (failures=1)
test test_csv failed -- Traceback (most recent call last):
File "/home2/buildbot/slave/trunk.loewis-sun/build/Lib/test/test_csv.py", line 607, in test_write_simple_dict
self.assertEqual(fileobj.read(), "10,,abc\r\n")
AssertionError: 'f1,f2,f3\r\n' != '10,,abc\r\n'
test_csv
test test_csv failed -- Traceback (most recent call last):
File "D:\cygwin\home\db3l\buildarea\trunk.bolen-windows\build\lib\test\test_csv.py", line 604, in test_write_simple_dict
writer.writerow({"f1": 10, "f3": "abc"})
File "D:\cygwin\home\db3l\buildarea\trunk.bolen-windows\build\lib\csv.py", line 148, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
IOError: [Errno 0] Error |
Testing on Windows with this: Index: Lib/test/test_csv.py --- Lib/test/test_csv.py (revision 78430)
+++ Lib/test/test_csv.py (working copy)
@@ -9,6 +9,7 @@
import tempfile
import csv
import gc
+import io
from test import test_support
class Test_Csv(unittest.TestCase):
@@ -595,7 +596,7 @@
### "short" means there are fewer elements in the row than fieldnames
def test_write_simple_dict(self):
fd, name = tempfile.mkstemp()
- fileobj = os.fdopen(fd, "w+b")
+ fileobj = io.open(fd, 'w+b')
try:
writer = csv.DictWriter(fileobj, fieldnames = ["f1", "f2", "f3"])
writer.writeheader() |
Committed in r78660 after positive comment from briancurtin re Windows. Hopefully this fixes Solaris, as well. |
Both the solaris and windows slaves seem to have succeeded this time. |
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
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: