Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]]
Expand Down