Skip to content

Commit 12a805b

Browse files
authored
Merge pull request #319 from airvzxf/bugfix/318/intfmt-string-values
Bug fix for issue #318 | `intfmt`: error for strings which contain integer values
2 parents 8946da0 + 23b3cc6 commit 12a805b

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

tabulate/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,17 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
12351235
if valtype is str:
12361236
return f"{val}"
12371237
elif valtype is int:
1238+
if isinstance(val, str):
1239+
val_striped = val.encode('unicode_escape').decode('utf-8')
1240+
colored = re.search(r'(\\[xX]+[0-9a-fA-F]+\[\d+[mM]+)([0-9.]+)(\\.*)$', val_striped)
1241+
if colored:
1242+
total_groups = len(colored.groups())
1243+
if total_groups == 3:
1244+
digits = colored.group(2)
1245+
if digits.isdigit():
1246+
val_new = colored.group(1) + format(int(digits), intfmt) + colored.group(3)
1247+
val = val_new.encode('utf-8').decode('unicode_escape')
1248+
intfmt = ""
12381249
return format(val, intfmt)
12391250
elif valtype is bytes:
12401251
try:

test/test_output.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test output of the various forms of tabular data."""
2+
from pytest import mark
23

34
import tabulate as tabulate_module
45
from common import assert_equal, raises, skip, check_warnings
@@ -2638,6 +2639,44 @@ def test_intfmt():
26382639
assert_equal(expected, result)
26392640

26402641

2642+
def test_intfmt_with_string_as_integer():
2643+
"Output: integer format"
2644+
result = tabulate([[82642], ["1500"], [2463]], intfmt=",", tablefmt="plain")
2645+
expected = "82,642\n 1500\n 2,463"
2646+
assert_equal(expected, result)
2647+
2648+
2649+
@mark.skip(reason="It detects all values as floats but there are strings and integers.")
2650+
def test_intfmt_with_string_with_floats():
2651+
"Output: integer format"
2652+
result = tabulate([[82000.38], ["1500.47"], ["2463"], [92165]], intfmt=",", tablefmt="plain")
2653+
expected = "82000.4\n 1500.47\n 2463\n92,165"
2654+
assert_equal(expected, result)
2655+
2656+
2657+
def test_intfmt_with_colors():
2658+
"Regression: Align ANSI-colored values as if they were colorless."
2659+
colortable = [
2660+
("\x1b[33mabc\x1b[0m", 42, "\x1b[31m42\x1b[0m"),
2661+
("\x1b[35mdef\x1b[0m", 987654321, "\x1b[32m987654321\x1b[0m"),
2662+
]
2663+
colorheaders = ("test", "\x1b[34mtest\x1b[0m", "test")
2664+
formatted = tabulate(colortable, colorheaders, "grid", intfmt=",")
2665+
expected = "\n".join(
2666+
[
2667+
"+--------+-------------+-------------+",
2668+
"| test | \x1b[34mtest\x1b[0m | test |",
2669+
"+========+=============+=============+",
2670+
"| \x1b[33mabc\x1b[0m | 42 | \x1b[31m42\x1b[0m |",
2671+
"+--------+-------------+-------------+",
2672+
"| \x1b[35mdef\x1b[0m | 987,654,321 | \x1b[32m987,654,321\x1b[0m |",
2673+
"+--------+-------------+-------------+",
2674+
]
2675+
)
2676+
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
2677+
assert_equal(expected, formatted)
2678+
2679+
26412680
def test_empty_data_with_headers():
26422681
"Output: table with empty data and headers as firstrow"
26432682
expected = ""

0 commit comments

Comments
 (0)