thombashi / pytablewriter

pytablewriter is a Python library to write a table in various formats: AsciiDoc / CSV / Elasticsearch / HTML / JavaScript / JSON / LaTeX / LDJSON / LTSV / Markdown / MediaWiki / NumPy / Excel / Pandas / Python / reStructuredText / SQLite / TOML / TSV.
https://pytablewriter.rtfd.io/
MIT License
610 stars 43 forks source link

Leading zeros are missing for strings in value_matrix #62

Closed jo-koe closed 9 months ago

jo-koe commented 10 months ago

Hi, first of all thanks a lot for creating this library!

I've just experienced that leading zeros of strings passed as part of the value_matrix are not printed when creating a Markdown table. Is this intended behaviour or do I miss something here?

Thanks a lot in advance, Jonas

jo-koe commented 10 months ago

Here's some simple code to reproduce the issue:

from pytablewriter import MarkdownTableWriter

def main():
    writer = MarkdownTableWriter(
        table_name="Simple Table",
        headers=[
            "String",
        ],
        value_matrix=[["0123456"]],
    )

    writer.write_table()

if __name__ == "__main__":
    main()

Output:

# Simple Table
|String|
|-----:|
|123456|

and if I change it to:

[...]
value_matrix=[["0123456" + "a"]],
[...]

the Output includes the missing "0":

# Simple Table
| String |
|--------|
|0123456a|
thombashi commented 9 months ago

@jo-koe The leading zeros will be printed as it is when you explicitly specify the type hint as str as follows:

def main():
    writer = MarkdownTableWriter(
        table_name="Simple Table",
        headers=[
            "String",
        ],
        type_hints=["str"],
        value_matrix=[["0123456"]],
    )

    writer.write_table()

if __name__ == "__main__":
    main()
|String |
|-------|
|0123456|

If you do not specify type_hints, the type of a column is automatically inferred from the values in the column. 0123456 is inferred as int, and the values will be converted to int and rendered as 123456. On the other hand, 0123456a is inferred as str, and the values will be rendered as 0123456a.