Feature classes and tables contain one or more columns of attribute information. You can get a list of the fields in a feature class through the ListFields()
function.
The ListFields()
function returns a list containing individual Field
objects for each field in a feature class or table. Some functions, such as ListFields()
and ListIndexes()
, require an input dataset to operate on. You can use a wildcard or field type to constrain the list that is returned. Each Field
object contains various read-only properties including Name
, AliasName
, Type
, Length
, and others.
Follow these steps to learn how to return a list of fields in a feature class.
C:ArcpyBookCh9ListOfFields.py
.arcpy
module:import arcpy
arcpy.env.workspace = "C:/ArcpyBook/data/CityOfSanAntonio.gdb"
ListFields()
method on the Burglary
feature class inside a try block:try: fieldList = arcpy.ListFields("Burglary")
for fld in fieldList: print("%s is a type of %s with a length of %i" % (fld.name, fld.type, fld.length))
Exception
block:except Exception as e: print(e.message)
import arcpy arcpy.env.workspace = "C:/ArcpyBook/data/CityOfSanAntonio.gdb" try: fieldList = arcpy.ListFields("Burglary") for fld in fieldList: print("%s is a type of %s with a length of %i" % (fld.name, fld.type, fld.length)) except Exception as e: print(e.message)
C:ArcpyBookcodeCh9ListOfFields.py
solution file.OBJECTID is a type of OID with a length of 4 Shape is a type of Geometry with a length of 0 CASE is a type of String with a length of 11 LOCATION is a type of String with a length of 40 DIST is a type of String with a length of 6 SVCAREA is a type of String with a length of 7 SPLITDT is a type of Date with a length of 8 SPLITTM is a type of Date with a length of 8 HR is a type of String with a length of 3 DOW is a type of String with a length of 3 SHIFT is a type of String with a length of 1 OFFCODE is a type of String with a length of 10 OFFDESC is a type of String with a length of 50 ARCCODE is a type of String with a length of 10 ARCCODE2 is a type of String with a length of 10 ARCTYPE is a type of String with a length of 10 XNAD83 is a type of Double with a length of 8 YNAD83 is a type of Double with a length of 8
The ListFields()
function returns a list of fields from a feature class or a table. This function accepts one required parameter, which is a reference to the feature class or table the function should be executed against. You can limit the fields returned by using a wildcard or a field type. In this recipe, we only specified a feature class that indicates that all the fields will be returned. For each field returned, we printed the name, field type, and field length. As I mentioned earlier when discussing the ListFeatureClasses()
function, ListFields()
and all the other list functions are often called as the first step in a multistep process within a script. For example, you might want to update the population statistics contained within a population
field for a census tract feature class. To do this, you could get a list of all the fields within a feature class, loop through this list by looking for a specific field name that contains information on the population, and then update the population information for each row. Alternatively, the ListFields()
function accepts a wildcard as one of its parameters, so if you already know the name of the population
field, you would pass this as the wildcard and only a single field will be returned.