Skip to content

Commit

Permalink
fix #192 - Can't print bytes with non-ascii encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
FurcyPin committed Sep 5, 2022
1 parent 3f0757e commit 1707234
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tabulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
elif valtype is bytes:
try:
return str(val, "ascii")
except TypeError:
except (TypeError, UnicodeDecodeError):
return str(val)
elif valtype is float:
is_a_colored_number = has_invisible and isinstance(val, (str, bytes))
Expand Down
10 changes: 10 additions & 0 deletions test/test_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,13 @@ def test_py37orlater_list_of_dataclasses_headers():
assert_equal(expected, result)
except ImportError:
skip("test_py37orlater_list_of_dataclasses_headers is skipped")


def test_list_bytes():
"Input: a list of bytes. (issue #192)"
lb = [["你好".encode("utf-8")], ["你好"]]
expected = "\n".join(
["bytes", "---------------------------", r"b'\xe4\xbd\xa0\xe5\xa5\xbd'", "你好"]
)
result = tabulate(lb, headers=["bytes"])
assert_equal(expected, result)

0 comments on commit 1707234

Please sign in to comment.