Skip navigation
All People > Dan_Patterson > Py... blog > 2016 > March > 01

Basic fancy formats...

 

Created:   2016-02-28

Modified: 2016-08-13

Attachments:

    Script attached in zip file below.

References:

 

This is a work in progress.

I am tired of reinventing the wheel every time I want to format output so it looks appealing and conveys the information I want.  To this end, I am going to present what I have... but I will do this in increments.

 

I will not provide commentary... I don't want to cloud the issue with rules and specifics.  I use a monospaced font to ensure that the output is what you would expect in any python IDE, and to control vertical alignment within a pure text environment.  Asample script is attached so you can experiment with your own ideas.

 

Consult the script header for further information.  The script also contains some format tips that arise from formatting script... which I have posted on previously.

 

Sample output (courier new 10pt)

This is the only font that will maintain monospacing from the available font family.  I formatted using the "Plain" syntax highlighter so that line numbers were created for reference purposes.

 

Basic formatting....

    :  |_{ [name/num] [!s, !r, none] [: format_spec]_|}
    :  |_{ vertical bar to denote start, _ optional spaces
    :  }_| vertical bar to denote end, _ optional spaces
    : ------------------------------------

    :   [name/num]  = empty, name, number
    :   [!s or !r]  = str or repr format or nothing
    :   [format]    = see examples in output

1   |{0:_>15.0f}|   - lead _, right, integer           |_________123456|

2   |{0:_>15.3f}|   - lead _, right, float             |_____123456.012|

3   |{0:_>+15.3f}|  - lead _, right, req. sign, float  |____+123456.012|

4   |{0:_> 15.3f}|  - lead _, right, opt. sign, float  |____ 123456.012|

5   |{0:> 15.3f}|   - no lead, right, opt. sign, float |     123456.012|

6   |{0:>15.3f}|    - whatever, float                  |     123456.012|

7   |{0: <15.3f}|   - left, float, trailing space(s)   |123456.012     |

8   |{0: < 15.3f}|  - left, space(s), float, spaces    | 123456.012    |

9   |{0:_< 15.3f}|  - left, space(s), float, __        | 123456.012____|

10  |{0!s:<15}|     - left, string                     |123456.01234   |

11  |{0!s:*<15}|    - left, string space(s)            |123456.01234***|

12  |{0!s:*^15}|    - center, lead/trail               |*123456.01234**|

13  |_{0!s: ^13}_|  - lead, center, trail              |_123456.01234 _|

14  |_{0!s:*<}_|    - no size, lead ignored, left      |_123456.01234_|

15  |_{0: >5f}_|    - float, undersized                |_123456.012340_|


Fancy formatting
....

Format elements are generated from field widths and content.
Some examples for ... 1234.5 ...

widths.... [6, 8, 10, 12]
building..

    f = [ "|_{{0!s:^{}}}_|".format(width) for width in widths ]
    txt = "".join(i for i in f)
    print(txt.format(1234.5))

formats... ['|_{0!s:^6}_|', '|_{0!s:^8}_|', '|_{0!s:^10}_|', '|_{0!s:^12}_|']

result.... |_1234.5_||_ 1234.5 _||1234.5  _||_   1234.5   _|


 

Comments welcome.

 

More to come....

Created:   2016-03-01

Modified:  2016-08-13

Attachments:

    Script attached in zip file below.

References:

 

 

This is a work in progress

This example shows how to generate column formats from the data itself and how to generate a header from the tabular information.  The simplest way to obtain a useful table for manipulation in other software is to use the FeatureclassToNumPyArray or TableToNumPyArray methods in arcpy.  A sample output for such a table is shown in the example.

 

In the example below, the following procedure was used:

  • the first row of the table is used to obtain the column widths and data type
  • an appropriate format string was used to create the column structure... NOTE. as in the previous example a vertical bar, |, is used to denote the column positional start and the underscore, _, to denote preceding or trailing spaces.
  • the format string and the number of columns in the table creates the line format,
  • combining that information with column width gives a resultant column format
  • an option to use the maximum size between the column name and data size is used to modify the values of column width.
  • finally... a column header...generated from the input data... is produced and the line is printed.

 

The next example will delve into how to do this all in one swoop giving the option to use column widths based upon data size with the columns and/or whether to maintain column size, alignment, record delimiters and whether to include ancillary information related to the table.

 

Format demo

 

Input array....

:.... [(0, [300000.0, 5025000.0], 12345, 1.2345, b'string', 'unicode')]

format string: |_{{!s:<{}}}_

column widths: [2, 21, 5, 6, 9, 7]

column type:   ['int32', 'ndarray', 'int32', 'float64', 'bytes_', 'str_']

data dtype:    ['i', 'f', 'i', 'f', 'S', 'U']

line format.

...  |_{{!s:<{}}}_|_{{!s:<{}}}_|_{{!s:<{}}}_|_{{!s:<{}}}_|_{{!s:<{}}}_|_{{!s:<{}}}_

resultant:..|_{!s:<2}_|_{!s:<21}_|_{!s:<5}_|_{!s:<6}_|_{!s:<9}_|_{!s:<7}_


Column header... |.ID.|..........XY...........|..C_0..|..C_1...|....C_2....|...C_3...

Output line..... |_0 _|_[  300000.  5025000.]_|_12345_|_1.2345_|_b'string'_|_unicode_


 

More later...