Skip to content

Commit b503408

Browse files
committed
deal with py27 and mixed int/str fields
1 parent 4ac8557 commit b503408

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

tabulate.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,10 @@ def _format(val, valtype, floatfmt, missingval=""):
445445
if valtype in [int, _text_type]:
446446
return "{0}".format(val)
447447
elif valtype is _binary_type:
448-
return _text_type(val, "ascii")
448+
try:
449+
return _text_type(val, "ascii")
450+
except TypeError:
451+
return _text_type(val)
449452
elif valtype is float:
450453
return format(float(val), floatfmt)
451454
else:

test/test_py2x.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""Python 2.x tests."""
4+
5+
from tabulate import tabulate
6+
from common import assert_equal
7+
8+
9+
def test_format_with_mixed_values():
10+
expected = '\n'.join([
11+
'-----',
12+
'',
13+
'a',
14+
'0',
15+
'False',
16+
'-----',
17+
])
18+
data = [[None], ['a'], [0], [False]]
19+
table = tabulate(data)
20+
assert_equal(table, expected)
21+

0 commit comments

Comments
 (0)