Fancy, fancy formatting

03-01-2016 05:38 AM
Labels (1)
MVP Esteemed Contributor
1 2 2,874

Created:   2016-03-01

Modified:  2016-08-13


    Script attached in zip file below.


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:<{}}}_


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...

MVP Regular Contributor

We added some fancy formatting to the email sent out by all of our Python scripts running as scheduled tasks.

# Opening lines of email message body
msg = "{scriptName} ran on {hostComputer}".format(
    scriptName = os.path.basename(__file__),
    hostComputer = socket.gethostname()
istring = " Started {} ".format("%Y-%m-%d %H:%M:%S"))
msg += "\n\n{:~^64}\n\n".format(istring)‍‍‍‍‍‍‍

 This creates the msg string which becomes the email body. The first two lines of the email always looks like this... ran on ServerName123

~~~~~~~~~~~~~~~~~ Started 2017-06-19 04:30:03 ~~~~~~~~~~~~~~~~~~‍‍‍

There's a similar line at the end of the email too.

MVP Esteemed Contributor

Nice example... formatting gets addictive

About the Author
Retired Geomatics Instructor at Carleton University. I am a forum MVP and Moderator. Current interests focus on python-based integration in GIS. See... Py... blog, my GeoNet blog...