[Python-checkins] r85856 - in python/branches/py3k: Doc/library/csv.rst Lib/csv.py Lib/test/test_csv.py Misc/NEWS

georg.brandl python-checkins at python.org
Wed Oct 27 09:27:06 CEST 2010


Author: georg.brandl
Date: Wed Oct 27 09:27:06 2010
New Revision: 85856

Log:
#5975: add unix_dialect to csv module.

Modified:
   python/branches/py3k/Doc/library/csv.rst
   python/branches/py3k/Lib/csv.py
   python/branches/py3k/Lib/test/test_csv.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Doc/library/csv.rst
==============================================================================
--- python/branches/py3k/Doc/library/csv.rst	(original)
+++ python/branches/py3k/Doc/library/csv.rst	Wed Oct 27 09:27:06 2010
@@ -187,6 +187,15 @@
    TAB-delimited file.  It is registered with the dialect name ``'excel-tab'``.
 
 
+.. class:: unix_dialect()
+
+   The :class:`unix_dialect` class defines the usual properties of a CSV file
+   generated on UNIX systems, i.e. using ``'\n'`` as line terminator and quoting
+   all fields.  It is registered with the dialect name ``'unix'``.
+
+   .. versionadded:: 3.2
+
+
 .. class:: Sniffer()
 
    The :class:`Sniffer` class is used to deduce the format of a CSV file.

Modified: python/branches/py3k/Lib/csv.py
==============================================================================
--- python/branches/py3k/Lib/csv.py	(original)
+++ python/branches/py3k/Lib/csv.py	Wed Oct 27 09:27:06 2010
@@ -20,7 +20,7 @@
             "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 @@
     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,

Modified: python/branches/py3k/Lib/test/test_csv.py
==============================================================================
--- python/branches/py3k/Lib/test/test_csv.py	(original)
+++ python/branches/py3k/Lib/test/test_csv.py	Wed Oct 27 09:27:06 2010
@@ -515,6 +515,15 @@
     def test_read_escape_fieldsep(self):
         self.readerAssertEqual('abc\\,def\r\n', [['abc,def']])
 
+class TestDialectUnix(TestCsvBase):
+    dialect = 'unix'
+
+    def test_simple_writer(self):
+        self.writerAssertEqual([[1, 'abc def', 'abc']], '"1","abc def","abc"\n')
+
+    def test_simple_reader(self):
+        self.readerAssertEqual('"1","abc def","abc"\n', [['1', 'abc def', 'abc']])
+
 class QuotedEscapedExcel(csv.excel):
     quoting = csv.QUOTE_NONNUMERIC
     escapechar = '\\'

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Wed Oct 27 09:27:06 2010
@@ -51,7 +51,9 @@
 Library
 -------
 
-- #7761: telnetlib.interact failures on Windows fixed.
+- Issue #5975: Add csv.unix_dialect class.
+
+- Issue #7761: telnetlib.interact failures on Windows fixed.
 
 - logging: Added style option to Formatter to allow %, {} or $-formatting.
 


More information about the Python-checkins mailing list