summaryrefslogtreecommitdiff
path: root/Lib/csv.py
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2011-03-16 11:35:38 +0200
committerEzio Melotti <ezio.melotti@gmail.com>2011-03-16 11:35:38 +0200
commita8fef3700df5400004ac88fa20c8bb4ff6f44ca3 (patch)
tree7745540e59b871e3d1b8b17757df99384cc91f68 /Lib/csv.py
parente21d50c5c74e58bdf7173ab8d8966cfae2004728 (diff)
parent6a6e1e572574894dd211325eed26d099bde4778d (diff)
downloadcpython-a8fef3700df5400004ac88fa20c8bb4ff6f44ca3.tar.gz
#11565: Merge with 3.1.
Diffstat (limited to 'Lib/csv.py')
-rw-r--r--Lib/csv.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/Lib/csv.py b/Lib/csv.py
index dbe6db7ead..8dfc77e310 100644
--- a/Lib/csv.py
+++ b/Lib/csv.py
@@ -20,7 +20,7 @@ __all__ = [ "QUOTE_MINIMAL", "QUOTE_ALL", "QUOTE_NONNUMERIC", "QUOTE_NONE",
"unregister_dialect", "__version__", "DictReader", "DictWriter" ]
class Dialect:
- """Describe an Excel dialect.
+ """Describe a CSV dialect.
This must be subclassed (see csv.excel). Valid attributes are:
delimiter, quotechar, escapechar, doublequote, skipinitialspace,
@@ -65,6 +65,16 @@ class excel_tab(excel):
delimiter = '\t'
register_dialect("excel-tab", excel_tab)
+class unix_dialect(Dialect):
+ """Describe the usual properties of Unix-generated CSV files."""
+ delimiter = ','
+ quotechar = '"'
+ doublequote = True
+ skipinitialspace = False
+ lineterminator = '\n'
+ quoting = QUOTE_ALL
+register_dialect("unix", unix_dialect)
+
class DictReader:
def __init__(self, f, fieldnames=None, restkey=None, restval=None,
@@ -127,6 +137,10 @@ class DictWriter:
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]