Print fields indexes of featur class

329
2
Jump to solution
04-20-2023 01:32 PM
2Quiker
Occasional Contributor II

How can you print out the field index and fields name of all the fields in a feature class?

print out something like this,

0 OBJECTID
1 Shape
2 GlobalID
3 field1
4 field2
5 field3
6 field4
7 field5
etc...

0 Kudos
1 Solution

Accepted Solutions
Mahdi_Ch
New Contributor III

 

 

import arcpy

#arcpy map read current project # can also pass the full path
aprx = arcpy.mp.ArcGISProject("CURRENT")

# List of FeatureClasses in Geodatabase
featureclasses = arcpy.ListFeatureClasses()

# the feature class that you want to check
featureclass_name = 'featureclass of interest'

# list of field (column) objects in the Attribute Table of the Feature Class
fields_ls = arcpy.ListFields(featureclass_name)  # object 
fields_names_ls= [f.name for f in fields_ls]  # name

for c,i in enumerate(fields_names_ls):
    print(f'{c:02}-{i}') # zero padded the index to match visually

 

This was a general example using Python. I zero padded the index to match them visually in case of having more than 10 fields. 

you can replace the last line by the following if that is not what you want: 

 

 print(f'{c} {i}')

 

List of feature classes was included just in case you need that, otherwise you can skip that line.

View solution in original post

2 Replies
Mahdi_Ch
New Contributor III

 

 

import arcpy

#arcpy map read current project # can also pass the full path
aprx = arcpy.mp.ArcGISProject("CURRENT")

# List of FeatureClasses in Geodatabase
featureclasses = arcpy.ListFeatureClasses()

# the feature class that you want to check
featureclass_name = 'featureclass of interest'

# list of field (column) objects in the Attribute Table of the Feature Class
fields_ls = arcpy.ListFields(featureclass_name)  # object 
fields_names_ls= [f.name for f in fields_ls]  # name

for c,i in enumerate(fields_names_ls):
    print(f'{c:02}-{i}') # zero padded the index to match visually

 

This was a general example using Python. I zero padded the index to match them visually in case of having more than 10 fields. 

you can replace the last line by the following if that is not what you want: 

 

 print(f'{c} {i}')

 

List of feature classes was included just in case you need that, otherwise you can skip that line.

ChrisRingo
Occasional Contributor

I'm not sure if you're talking about an automated way to do this, but for a quick solution, I usually just right-click the layer / Data Design / Fields. Then click the upper left of that table (with the small triangle) to select everything, then right-click again to copy, then just paste it into Excel. You'll have to add your own "field index" - I don't know of any view in ArcGIS that gives a field "index"

In a python window you could do something this:

i=0
for f in arcpy.ListFields(feature_class):
    print(str(i) + " " + f.name)
    i+=1
0 Kudos