pretty print fields

This is a piece of code i’ve written and re-written dozens of times. Typically I code it on a whim for a single use.

Today I went the extra mile and made the output really nice, so here it is in case somebody has use for it.

And this is what you get as output in the python window*.
pywin_pprint

* this is the nice and new ArcGIS Pro’s python window.

The output is a bit too wide for use in terminal (80 characters) but within the app it’s nice and clear.

Happy coding.

2 thoughts on “pretty print fields

  1. Hi there, this is nice code. Just thought I’d mention that after I found myself reusing the same snippet many times, I just created my own little python module in the python site-packages folder, so I could call my little custom functions anytime from anywhere. One I do all the time is

    from mymodule import TakeOutTrash

    TakeOutTrash(“fl”)

    Inside of the C:\Python27\Lib\site-packages\mymodule\__init__.py file I have a function that looks like this:

    def TakeOutTrash(dataset):
    if arcpy.Exists(dataset):
    arcpy.management.Delete(dataset)

    I just got tired of writing that if statement all the time. I have a similar function in there that will make a list full paths to all of the feature classes in a geodatabase, including those within feature class datasets. Little things like that have made my work a ton easier.

    Cheers,
    Adam

  2. Here’s my slight modification that adds some handy tweaks

    def pprint_fields(table, wild=None, detail=False):
    “”” pretty print table’s fields and their properties “””
    def _print(l):
    print(“”.join([“{:>12}”.format(i) for i in l]))
    atts = [‘name’, ‘aliasName’, ‘type’, ‘baseName’]
    if detail:
    atts += [‘domain’, ‘editable’, ‘isNullable’, ‘length’, ‘precision’,
    ‘required’, ‘scale’]
    _print(atts)
    for f in arcpy.ListFields(table, wild):
    _print([“{:>12}”.format(getattr(f, i)) for i in atts])

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s