diff options
| author | Bruno Alla <bruno.alla@founders4schools.org.uk> | 2017-01-12 09:49:45 +0000 |
|---|---|---|
| committer | Bruno Alla <bruno.alla@founders4schools.org.uk> | 2017-05-02 17:33:14 +0100 |
| commit | 80e72cfa27264efb9f525bd92ce6476c5eadb3e9 (patch) | |
| tree | 5bcba41ed2b45c5e063d00a0676ae9400a582417 /tablib | |
| parent | 05bd0d1d42e2a1aeb3b835a4ae96de7364735192 (diff) | |
| download | tablib-80e72cfa27264efb9f525bd92ce6476c5eadb3e9.tar.gz | |
Fix unicode encode errors on Python 2 -- Fixes #215
Switch csv library to backports.csv as the implementation
is closer to the python 3 one. Add a test case covering the
problem.
Run tests with unicode_literals from future
Fix unicode encode errors with unicode characters
- Use `backports.csv` instead of `unicodecsv`
- Use StringIO instead of cStringIO
- Clean-up some Python 2 specific code
Diffstat (limited to 'tablib')
| -rw-r--r-- | tablib/compat.py | 4 | ||||
| -rw-r--r-- | tablib/core.py | 2 | ||||
| -rw-r--r-- | tablib/formats/_csv.py | 9 | ||||
| -rw-r--r-- | tablib/formats/_tsv.py | 4 |
4 files changed, 7 insertions, 12 deletions
diff --git a/tablib/compat.py b/tablib/compat.py index 43e0bbc..d18a781 100644 --- a/tablib/compat.py +++ b/tablib/compat.py @@ -37,11 +37,11 @@ if is_py3: else: from cStringIO import StringIO as BytesIO - from cStringIO import StringIO + from StringIO import StringIO from tablib.packages import markup from itertools import ifilter - import unicodecsv as csv + from backports import csv import tablib.packages.dbfpy as dbfpy unicode = unicode diff --git a/tablib/core.py b/tablib/core.py index c44c6ac..b97da54 100644 --- a/tablib/core.py +++ b/tablib/core.py @@ -831,7 +831,7 @@ class Dataset(object): against each cell value. """ - if isinstance(col, str): + if isinstance(col, unicode): if col in self.headers: col = self.headers.index(col) # get 'key' index from each data else: diff --git a/tablib/formats/_csv.py b/tablib/formats/_csv.py index 994b23b..b74afd7 100644 --- a/tablib/formats/_csv.py +++ b/tablib/formats/_csv.py @@ -3,15 +3,14 @@ """ Tablib - *SV Support. """ -from tablib.compat import is_py3, csv, StringIO +from tablib.compat import csv, StringIO, unicode title = 'csv' extensions = ('csv',) -DEFAULT_ENCODING = 'utf-8' -DEFAULT_DELIMITER = ',' +DEFAULT_DELIMITER = unicode(',') def export_set(dataset, **kwargs): @@ -19,8 +18,6 @@ def export_set(dataset, **kwargs): stream = StringIO() kwargs.setdefault('delimiter', DEFAULT_DELIMITER) - if not is_py3: - kwargs.setdefault('encoding', DEFAULT_ENCODING) _csv = csv.writer(stream, **kwargs) @@ -36,8 +33,6 @@ def import_set(dset, in_stream, headers=True, **kwargs): dset.wipe() kwargs.setdefault('delimiter', DEFAULT_DELIMITER) - if not is_py3: - kwargs.setdefault('encoding', DEFAULT_ENCODING) rows = csv.reader(StringIO(in_stream), **kwargs) for i, row in enumerate(rows): diff --git a/tablib/formats/_tsv.py b/tablib/formats/_tsv.py index 9380b3b..1c6d6a1 100644 --- a/tablib/formats/_tsv.py +++ b/tablib/formats/_tsv.py @@ -3,6 +3,7 @@ """ Tablib - TSV (Tab Separated Values) Support. """ +from tablib.compat import unicode from tablib.formats._csv import ( export_set as export_set_wrapper, import_set as import_set_wrapper, @@ -12,8 +13,7 @@ from tablib.formats._csv import ( title = 'tsv' extensions = ('tsv',) -DEFAULT_ENCODING = 'utf-8' -DELIMITER = '\t' +DELIMITER = unicode('\t') def export_set(dataset): """Returns TSV representation of Dataset.""" |
