Comma-separate thousands or custom formatting in a column?
See original GitHub issueimport pytablewriter
data = [
{"category": "2.6", "date": "2018-08-15", "downloads": 51},
{"category": "2.7", "date": "2018-08-15", "downloads": 63749},
{"category": "3.2", "date": "2018-08-15", "downloads": 2},
{"category": "3.3", "date": "2018-08-15", "downloads": 40},
{"category": "3.4", "date": "2018-08-15", "downloads": 6095},
{"category": "3.5", "date": "2018-08-15", "downloads": 20358},
{"category": "3.6", "date": "2018-08-15", "downloads": 35274},
{"category": "3.7", "date": "2018-08-15", "downloads": 6595},
{"category": "3.8", "date": "2018-08-15", "downloads": 3},
{"category": "null", "date": "2018-08-15", "downloads": 1019},
]
writer = pytablewriter.MarkdownTableWriter()
writer.margin = 1
writer.header_list = sorted(set().union(*(d.keys() for d in data)))
writer.value_matrix = data
writer.write_table()
Produces:
category | date | downloads |
---|---|---|
2.6 | 2018-08-15 | 51 |
2.7 | 2018-08-15 | 63749 |
3.2 | 2018-08-15 | 2 |
3.3 | 2018-08-15 | 40 |
3.4 | 2018-08-15 | 6095 |
3.5 | 2018-08-15 | 20358 |
3.6 | 2018-08-15 | 35274 |
3.7 | 2018-08-15 | 6595 |
3.8 | 2018-08-15 | 3 |
null | 2018-08-15 | 1019 |
I’d like the downloads to have comma-separated thousands to increase readability:
category | date | downloads |
---|---|---|
2.6 | 2018-08-15 | 51 |
2.7 | 2018-08-15 | 63,749 |
3.2 | 2018-08-15 | 2 |
3.3 | 2018-08-15 | 40 |
3.4 | 2018-08-15 | 6,095 |
3.5 | 2018-08-15 | 20,358 |
3.6 | 2018-08-15 | 35,274 |
3.7 | 2018-08-15 | 6,595 |
3.8 | 2018-08-15 | 3 |
null | 2018-08-15 | 1,019 |
I can preprocess the data:
new_data = []
for row in data:
row["downloads"] = "{:,}".format(row["downloads"])
new_data.append(row)
data = new_data
category | date | downloads |
---|---|---|
2.6 | 2018-08-15 | 51 |
2.7 | 2018-08-15 | 63,749 |
3.2 | 2018-08-15 | 2 |
3.3 | 2018-08-15 | 40 |
3.4 | 2018-08-15 | 6,095 |
3.5 | 2018-08-15 | 20,358 |
3.6 | 2018-08-15 | 35,274 |
3.7 | 2018-08-15 | 6,595 |
3.8 | 2018-08-15 | 3 |
null | 2018-08-15 | 1,019 |
And then explicitly right-format the column:
from pytablewriter import Align
writer.align_list = [Align.AUTO, Align.AUTO, Align.RIGHT]
# Or more generically:
writer.align_list = [Align.AUTO] * len(writer.header_list)
download_index = writer.header_list.index("downloads")
writer.align_list[download_index] = Align.RIGHT
category | date | downloads |
---|---|---|
2.6 | 2018-08-15 | 51 |
2.7 | 2018-08-15 | 63,749 |
3.2 | 2018-08-15 | 2 |
3.3 | 2018-08-15 | 40 |
3.4 | 2018-08-15 | 6,095 |
3.5 | 2018-08-15 | 20,358 |
3.6 | 2018-08-15 | 35,274 |
3.7 | 2018-08-15 | 6,595 |
3.8 | 2018-08-15 | 3 |
null | 2018-08-15 | 1,019 |
But is there a neater way? Would some option to format numbers with comma-separated thousands be possible or sensible? Or perhaps custom formatting?
from pytablewriter import Format
writer.format_list = [None, None, Format.Comma]
writer.format_list = [None, None, "{:,}"]
That could cover cases that format numbers like 12 345.6
or 12.345,6
, and also allow other formatting options.
Thank you.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (8 by maintainers)
Top Results From Across the Web
How to Apply Comma Style in Excel (Step by Step Tutorial)
The comma style in Excel is also known as the thousand separator format that converts the large number values with the commas inserted...
Read more >Custom Excel number format - Ablebits
To create an Excel custom number format with a thousands separator, include a comma (,) in the format code. For example:.
Read more >Excel Custom Number Format Millions and Thousands
Large numbers in Excel can be formatted so they can be shown in ... CUSTOM and then enter one comma to show Thousands...
Read more >Comma Style in Excel | How to Apply? | Shortcut Keys to Use
The comma style format (also known as the thousands separator) regularly goes with the “Accounting” format. Like the “Accounting” position, the comma ......
Read more >Show or hide the thousands separator - Microsoft Support
Show or hide the thousands separator · Select the cells that you want to format. · On the Home tab, click the Dialog...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@hugovk Thank you for your feedback. Surely, number formatting specifier interface would be a nice feature. I will consider the feature for the future release (probably the next release).
I integrated
format_list
attribute intostyle_list
attribute atpytablewriter 0.37.0
.format_list
can still be used (may be removed in the future release).https://pytablewriter.readthedocs.io/en/latest/pages/examples/style/index.html