propertiesTextBlock = """
Basic Properties
----------------
- nodes (n) {n}
- edges (m) {m}
- min. degree {minDeg}
- max. degree {maxDeg}
- isolated nodes {isolates}
- self-loops {loops}
- density {dens:10.6f}
"""
Several data items are inserted using string.format
. Output to console then looks like:
Basic Properties
----------------
- nodes (n) 10680
- edges (m) 24316
- min. degree 1
- max. degree 205
- isolated nodes 0
- self-loops 0
- density 0.000426
Not perfect, because I need to manually insert exactly the right amount of tabs in the text block. Also, what if I want to align the numbers differently (e.g right-justify everything, align at .
...)
Is there an easy way to make sure that this table looks nice?
You can use the format mini language to specify alignments:
The
>20
formatting specification sets the field width to 20 characters and right-aligns the value in that field.This does not support alignment by decimal point, however. You can specify dynamic field widths though:
which you can adapt to add or remove whitespace around a floating point number:
Here the field width is adjusted to shift the decimal point left or right based on how much space the whole value is going to take. Note that the field width is the total width, so including the decimal point and the 6 decimals.
The right answer is maybe to use
prettytable
ortabulate
.If you want to stay with plain old format, you could control field width:
For float values you could align to the decimal point:
Here is the formal description of the "format mini-language": http://docs.python.org/2/library/string.html#formatstrings