diff --git a/tabulate/__init__.py b/tabulate/__init__.py index 12a2950..a602694 100644 --- a/tabulate/__init__.py +++ b/tabulate/__init__.py @@ -2287,13 +2287,16 @@ def tabulate( list_of_lists, headers = _rst_escape_first_column(list_of_lists, headers) # PrettyTable formatting does not use any extra padding. - # Numbers are not parsed and are treated the same as strings for alignment. + # Numbers are not parsed and are treated the same as strings for alignment, + # unless the user explicitly asks for numbers to be reformatted by passing + # a custom floatfmt or intfmt (issue #71). # Check if pretty is the format being used and override the defaults so it # does not impact other formats. min_padding = MIN_PADDING if tablefmt == "pretty": min_padding = 0 - disable_numparse = True + if floatfmt == _DEFAULT_FLOATFMT and intfmt == _DEFAULT_INTFMT: + disable_numparse = True numalign = "center" if numalign == _DEFAULT_ALIGN else numalign stralign = "center" if stralign == _DEFAULT_ALIGN else stralign else: diff --git a/test/test_output.py b/test/test_output.py index ea3da87..221149d 100644 --- a/test/test_output.py +++ b/test/test_output.py @@ -2288,6 +2288,37 @@ def test_pretty_headerless(): assert_equal(expected, result) +def test_pretty_floatfmt(): + "Output: pretty respects an explicitly given floatfmt (issue #71)" + expected = "\n".join( + [ + "+---------+---------+", + "| strings | numbers |", + "+---------+---------+", + "| spam | 42.00 |", + "| eggs | 451.00 |", + "+---------+---------+", + ] + ) + result = tabulate( + _test_table, _test_table_headers, tablefmt="pretty", floatfmt=".2f" + ) + assert_equal(expected, result) + + +def test_pretty_intfmt(): + "Output: pretty respects an explicitly given intfmt (issue #71)" + expected = "\n".join( + [ + "+-----+", + "| 042 |", + "+-----+", + ] + ) + result = tabulate([[42]], tablefmt="pretty", intfmt="03d") + assert_equal(expected, result) + + def test_pretty_multiline_headerless(): "Output: pretty with multiline cells without headers" table = [["foo bar\nbaz\nbau", "hello"], ["", "multiline\nworld"]]