Skip to content

Commit

Permalink
Handle unicode problems and python2/3 compatibility in csv module
Browse files Browse the repository at this point in the history
  • Loading branch information
mcraig-ibme committed Dec 12, 2018
1 parent 11ce3f7 commit 39a3d12
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions oxasl/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 39a3d12

Please sign in to comment.