added unicode support for TSV for pytjhon 2

This commit is contained in:
Kunal Khandelwal 2018-06-25 17:12:59 +05:30
parent 9696733fd1
commit 5a7c35518b
4 changed files with 25 additions and 0 deletions

View File

@ -164,6 +164,12 @@ class TSV(TextFormat):
TABLIB_MODULE = 'tablib.formats._tsv'
CONTENT_TYPE = 'text/tab-separated-values'
def create_dataset(self, in_stream, **kwargs):
if sys.version_info[0] < 3:
# python 2.7 csv does not do unicode
return super(TSV, self).create_dataset(in_stream.encode('utf-8'), **kwargs)
return super(TSV, self).create_dataset(in_stream, **kwargs)
class ODS(TextFormat):
TABLIB_MODULE = 'tablib.formats._ods'

View File

@ -0,0 +1,2 @@
id name author_email
1 Some bookš test@example.com
1 id name author_email
2 1 Some bookš test@example.com

View File

@ -74,3 +74,20 @@ class CSVTest(TestCase):
with open(filename, self.format.get_read_mode()) as in_stream:
data = force_text(in_stream.read())
base_formats.CSV().create_dataset(data)
class TSVTest(TestCase):
def setUp(self):
self.format = base_formats.CSV()
def test_import_unicode(self):
# importing tsv UnicodeEncodeError
filename = os.path.join(
os.path.dirname(__file__),
os.path.pardir,
'exports',
'books-unicode.tsv')
with open(filename, self.format.get_read_mode()) as in_stream:
data = force_text(in_stream.read())
base_formats.CSV().create_dataset(data)

Binary file not shown.