Skip to content

Instantly share code, notes, and snippets.

@indivisible
Created May 11, 2017 17:18
Show Gist options
  • Save indivisible/baa7e21e4f6d66d1b123d42fef0ad7ac to your computer and use it in GitHub Desktop.
Save indivisible/baa7e21e4f6d66d1b123d42fef0ad7ac to your computer and use it in GitHub Desktop.
$ python3 unicode_test.py
Running test on 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118]
Running with locale encoding 'UTF-8'...
Success
$ python2 unicode_test.py
Running test on 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118]
Running with locale encoding 'UTF-8'...
Success
$ LC_ALL=en_US python2 unicode_test.py
Running test on 2.7.13 (default, Jan 19 2017, 14:48:08)
[GCC 6.3.0 20170118]
Running with locale encoding 'ANSI_X3.4-1968'...
Success
$ LC_ALL=en_US python3 unicode_test.py
Running test on 3.5.3 (default, Jan 19 2017, 14:11:04)
[GCC 6.3.0 20170118]
Running with locale encoding 'ANSI_X3.4-1968'...
Traceback (most recent call last):
File "unicode_test.py", line 19, in <module>
test_str_csv()
File "unicode_test.py", line 14, in test_str_csv
return pivot_ui(df)
File "/home/malac/jupyter_pivottablejs/pivottablejs/__init__.py", line 75, in pivot_ui
dict(csv=df.to_csv(), kwargs=json.dumps(kwargs)))
UnicodeEncodeError: 'ascii' codec can't encode character '\xe1' in position 3124: ordinal not in range(128)
diff --git a/pivottablejs/__init__.py b/pivottablejs/__init__.py
index 64c2b2f..7711c3f 100644
--- a/pivottablejs/__init__.py
+++ b/pivottablejs/__init__.py
@@ -1,4 +1,4 @@
-TEMPLATE = """
+TEMPLATE = u"""
<!DOCTYPE html>
<html>
<head>
@@ -67,11 +67,15 @@ TEMPLATE = """
from IPython.display import IFrame
import json
+import io
def pivot_ui(df, outfile_path = "pivottablejs.html", url="",
width="100%", height="500", **kwargs):
- with open(outfile_path, 'w') as outfile:
+ with io.open(outfile_path, 'wt', encoding='utf8') as outfile:
+ csv = df.to_csv(encoding='utf8')
+ if hasattr(csv, 'decode'):
+ csv = csv.decode('utf8')
outfile.write(TEMPLATE %
- dict(csv=df.to_csv(), kwargs=json.dumps(kwargs)))
+ dict(csv=csv, kwargs=json.dumps(kwargs)))
return IFrame(src=url or outfile_path, width=width, height=height)
#!/usr/bin/env python3
import locale
import pandas as pd
import io
from pivottablejs import pivot_ui
def test_str_csv():
print ('Running with locale encoding {!r}...'.format(locale.getpreferredencoding()))
csv_data = u',\xe1rv\xedzt\u0171r\u0151 t\xfck\xf6rf\xfar\xf3g\xe9p\n0,42\n'
csv = io.StringIO(csv_data)
df = pd.read_csv(csv)
return pivot_ui(df)
if __name__ == '__main__':
import sys
print ('Running test on {}'.format(sys.version))
test_str_csv()
print ('Success')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment