Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Created March 24, 2018 07:54
Show Gist options
  • Save AlexArcPy/49851e1ebbb37746bccec6ecf2c3d7ac to your computer and use it in GitHub Desktop.
Save AlexArcPy/49851e1ebbb37746bccec6ecf2c3d7ac to your computer and use it in GitHub Desktop.
Printing pretty tables with Python in ArcGIS
# using the built-in Python modules
import arcpy
arcpy.env.workspace = r'C:\Program Files (x86)\ArcGIS\Desktop10.5\TemplateData\TemplateData.gdb'
fc = r'USA\cities'
data = ['CITY_FIPS', 'CITY_NAME', 'STATE_FIPS', 'STATE_NAME', 'POP1990']
feats = [f for f in arcpy.da.SearchCursor(fc, field_names=data)][:10]
arcpy.AddMessage(" ")
row_format = u"{:>20}" * (len(data))
arcpy.AddMessage(row_format.format(*data))
for feat in feats:
arcpy.AddMessage(row_format.format(*feat))
arcpy.AddMessage(" ")
# using 3rd party tabulate module
import arcpy
from tabulate import tabulate
arcpy.env.workspace = r'C:\Program Files (x86)\ArcGIS\Desktop10.5\TemplateData\TemplateData.gdb'
fc = r'USA\cities'
data = [['CITY_FIPS', 'CITY_NAME', 'STATE_FIPS', 'STATE_NAME', 'POP1990']]
feats = [f for f in arcpy.da.SearchCursor(fc, field_names=data[0])][:10]
data.extend(feats)
arcpy.AddMessage(" ")
arcpy.AddMessage(tabulate(data))
arcpy.AddMessage(" ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment