]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#5975: add unix_dialect to csv module.
authorGeorg Brandl <georg@python.org>
Wed, 27 Oct 2010 07:27:06 +0000 (07:27 +0000)
committerGeorg Brandl <georg@python.org>
Wed, 27 Oct 2010 07:27:06 +0000 (07:27 +0000)
Doc/library/csv.rst
Lib/csv.py
Lib/test/test_csv.py
Misc/NEWS

index 8ca17415e6c4f7ae25700baaa157154b3f0fa6d9..ea1834972173108532111e33ef4041f6b9745f58 100644 (file)
@@ -187,6 +187,15 @@ The :mod:`csv` module defines the following classes:
    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.
index 5ae5a730468ff6bc8a4bf4047973c56277f1900c..e0f47e8b5d365af59abd8f205bd3bd373b22afdd 100644 (file)
@@ -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,
index 97800afa693a1bb2aa5c217766db37fae8aa0579..c3da185e1051caca7988cb02930f52df49a5e55e 100644 (file)
@@ -515,6 +515,15 @@ class TestEscapedExcel(TestCsvBase):
     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 = '\\'
index 8fb6203a89995874612095ec164b157168927d39..d860a7abec837ef11aa465a24f20283cbf92c6ba 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -51,7 +51,9 @@ Core and Builtins
 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.