From 39a3d122c6e585befafae25b6b969e48dcb7038a Mon Sep 17 00:00:00 2001 From: Martin Craig Date: Wed, 12 Dec 2018 13:52:30 +0000 Subject: [PATCH] Handle unicode problems and python2/3 compatibility in csv module --- oxasl/reporting.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/oxasl/reporting.py b/oxasl/reporting.py index 6c2447b..e46d96e 100644 --- a/oxasl/reporting.py +++ b/oxasl/reporting.py @@ -253,10 +253,18 @@ def table(self, tabdata, name="", headers=None): csvtxt = six.StringIO() writer = csv.writer(csvtxt) - for row in tabdata: - writer.writerow([unicode(s).encode("utf-8") for s in row]) - for line in csvtxt.getvalue().splitlines(): - self._content += " " + line.decode("utf-8") + "\n" + # Required because csv does not support unicode in python 2 + if six.PY2: + for row in tabdata: + writer.writerow([unicode(s).encode("utf-8") for s in row]) + for line in csvtxt.getvalue().splitlines(): + self._content += " " + line.decode("utf-8") + "\n" + else: + for row in tabdata: + writer.writerow([str(s) for s in row]) + for line in csvtxt.getvalue().splitlines(): + self._content += " " + line + "\n" + self._content += "\n" def dicttable(self, dictionary): @@ -267,7 +275,7 @@ def tofile(self, fname): """ Write RST content to a file """ - with open(fname, "w") as rstfile: + with open(fname, "wb") as rstfile: rstfile.write(self._content.encode("utf-8")) def _latex_float(self, f, sf=3):