prjemian / pyRestTable

Format a nice table in reST (reStructuredText) from Python
http://prjemian.github.io/pyRestTable/
2 stars 2 forks source link

Simplify presentation of key:value dictionary #76

Open prjemian opened 3 weeks ago

prjemian commented 3 weeks ago

For a simple key:value dictionary, such as:

dd = {'a': 1, 'b': 'bb'}

Make it easier to build this table:

=== =====
key value
=== =====
a   1    
b   bb   
=== =====
prjemian commented 3 weeks ago

suggest this code example:

def kv_table(kvdict):
    table = pyRestTable.Table()
    table.labels = "key value".split()
    table.rows = [(k, v) for k, v in kvdict.items()]
    return table
prjemian commented 3 weeks ago

Note that the existing dict_to_table() method is for a different data model, where the keys are column labels and len(each_value) > 0 and the same for all keys. (Such as a named data arrays.)