Skip to content

Commit

Permalink
Merge branch 'bugfix/wrapper_encoding' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
GjjvdBurg committed Oct 15, 2020
2 parents 775c558 + 9f930e0 commit 15e157e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
12 changes: 10 additions & 2 deletions clevercsv/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ def detect_dialect(
return dialect


def write_table(table, filename, dialect="excel", transpose=False):
def write_table(
table, filename, dialect="excel", transpose=False, encoding=None
):
"""Write a table (a list of lists) to a file
This is a convenience function for writing a table to a CSV file.
Expand All @@ -441,6 +443,12 @@ def write_table(table, filename, dialect="excel", transpose=False):
transpose : bool
Transpose the table before writing.
encoding : str
Encoding to use to write the data to the file. Note that the default
encoding is platform dependent, which ensures compatibility with the
Python open() function. It thus defaults to
`locale.getpreferredencoding()`.
Raises
------
ValueError:
Expand All @@ -454,6 +462,6 @@ def write_table(table, filename, dialect="excel", transpose=False):
if len(set(map(len, table))) > 1:
raise ValueError("Table doesn't have constant row length.")

with open(filename, "w", newline="") as fp:
with open(filename, "w", newline="", encoding=encoding) as fp:
w = writer(fp, dialect=dialect)
w.writerows(table)
22 changes: 16 additions & 6 deletions tests/test_unit/test_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,11 @@ def test_stream_table(self):
with self.assertRaises(NoDetectionResult):
self._stream_test_rows(rows, exp)

def _write_test(self, table, expected, dialect="excel", transpose=False):
def _write_test(self, table, expected, **kwargs):
tmpfd, tmpfname = tempfile.mkstemp(prefix="ccsv_", suffix=".csv")
wrappers.write_table(
table, tmpfname, dialect=dialect, transpose=transpose
)
with open(tmpfname, "r") as fp:
wrappers.write_table(table, tmpfname, **kwargs)
read_encoding = kwargs.get("encoding", None)
with open(tmpfname, "r", newline="", encoding=read_encoding) as fp:
data = fp.read()

try:
Expand All @@ -204,7 +203,7 @@ def _write_test(self, table, expected, dialect="excel", transpose=False):

def test_write(self):
table = [["A", "B,C", "D"], [1, 2, 3], [4, 5, 6]]
exp = 'A,"B,C",D\n1,2,3\n4,5,6\n'
exp = 'A,"B,C",D\r\n1,2,3\r\n4,5,6\r\n'
with self.subTest(name="default"):
self._write_test(table, exp)

Expand All @@ -220,3 +219,14 @@ def test_write(self):
table[2].append(8)
with self.assertRaises(ValueError):
self._write_test(table, "")

table = [["Å", "B", "C"], [1, 2, 3], [4, 5, 6]]
exp = "Å,B,C\r\n1,2,3\r\n4,5,6\r\n"
with self.subTest(name="encoding_1"):
# Not specifying an encoding here could potentially fail on
# Windows, due to open() defaulting to
# locale.getpreferredencoding() (see gh-27).
self._write_test(table, exp, encoding="utf-8")

with self.subTest(name="encoding_2"):
self._write_test(table, exp, encoding="cp1252")

0 comments on commit 15e157e

Please sign in to comment.