Getting a list of fields in a feature class or table

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.

Getting ready

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.

How to do it…

Follow these steps to learn how to return a list of fields in a feature class.

  1. Open IDLE and create a new script window.
  2. Save the script as C:ArcpyBookCh9ListOfFields.py.
  3. Import the arcpy module:
    import arcpy
  4. Set the workspace:
    arcpy.env.workspace = "C:/ArcpyBook/data/CityOfSanAntonio.gdb"
  5. Call the ListFields() method on the Burglary feature class inside a try block:
    try:
        fieldList = arcpy.ListFields("Burglary")
  6. Loop through each of the fields in the list of fields and print out the name, type, and length. Make sure you indent as needed:
      for fld in fieldList:
        print("%s is a type of %s with a length of %i" % (fld.name, fld.type, fld.length))
  7. Add the Exception block:
    except Exception as e:
        print(e.message)
  8. The entire script should appear as follows:
    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)
  9. You can check your work by examining the C:ArcpyBookcodeCh9ListOfFields.py solution file.
  10. Save and run the script. You should see the following output:
    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
    

How it works…

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset