Merge pull request #794 from kunal15595/develop

added unicode support for TSV for python 2
This commit is contained in:
Bojan Mihelac 2018-06-27 09:01:51 +02:00 committed by GitHub
commit 9978865dc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 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

@ -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,31 @@ 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.TSV()
def test_import_mac(self):
filename = os.path.join(
os.path.dirname(__file__),
os.path.pardir,
'exports',
'books-mac.tsv')
with open(filename, self.format.get_read_mode()) as in_stream:
actual = in_stream.read()
expected = 'id\tname\tauthor_email\n1\tSome book\ttest@example.com\n'
self.assertEqual(actual, expected)
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.TSV().create_dataset(data)

Binary file not shown.