summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2010-10-27 07:27:06 (GMT)
committerGeorg Brandl <georg@python.org>2010-10-27 07:27:06 (GMT)
commit7424dd359c9ffcd9a2b866f62fafda6d3a99b4b1 (patch)
treef3cd5d315c397839b4ab2c96612708c0b73f67dd /Lib
parent347fe5ce3c119f30bd228d5e2678fee5013ca052 (diff)
downloadcpython-7424dd359c9ffcd9a2b866f62fafda6d3a99b4b1.zip
cpython-7424dd359c9ffcd9a2b866f62fafda6d3a99b4b1.tar.gz
cpython-7424dd359c9ffcd9a2b866f62fafda6d3a99b4b1.tar.bz2
#5975: add unix_dialect to csv module.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/csv.py12
-rw-r--r--Lib/test/test_csv.py9
2 files changed, 20 insertions, 1 deletions
diff --git a/Lib/csv.py b/Lib/csv.py
index 5ae5a73..e0f47e8 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,
diff --git a/Lib/test/test_csv.py b/Lib/test/test_csv.py
index 97800af..c3da185 100644
--- a/Lib/test/test_csv.py
+++ b/Lib/test/test_csv.py
@@ -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 = '\\'