7

I have a table:

enter image description here ,

Script at ModelBuilder's "calculate values":

Expression:

a("%path%")

Code BLOCK:

def a(path):
  fields = arcpy.ListFields(path,"FID_*")
  for field in fields:
    return field.name

Gives me only ONE field name!

5 Answers 5

16

Yes. The return statement short-circuits it. Basic Python.

def a(path):
  field_names = []
  fields = arcpy.ListFields(path,"FID_*")
  for field in fields:
    field_names.append(field.name)
  return field_names

Though you may need to do it this way:

def a(path):
  field_names = []
  fields = arcpy.ListFields(path,"FID_*")
  for field in fields:
    field_names.append(field.name)
  return ";".join(field_names)
2
  • 6
    Just to reinforce that, when Python encounters a return statement, it will always exit the function, regardless of a loop.
    – sgrieve
    Sep 27, 2012 at 7:38
  • I use arcpy.ListFields(fc,"*","All") where fc is the target feature class. Sep 27, 2012 at 19:30
10
    def getFieldNames(shp):
        fieldnames = [f.name for f in arcpy.ListFields(shp)]
        return fieldnames

    fieldnames = getFieldNames("country.shp")

returns a list of all fields of country.shp

0
def a(path):
  fields = arcpy.ListFields(path,"FID_*")
  return fields
0
0

You can work with a simple line for loop surch in accordance to the documentation

def fieldnames (dataset, sep=",", filter="*", fields_type="All"):
    """ Return Str of fields in a feature class, shapefile, or table
    in a specified dataset. The returned str can be limited with
    search criteria for name and field type and will contain field objects."""

    return sep.join(list([f.name for f in arcpy.ListFields(dataset, filter, fields_type)]))

and just call

fiednames("%path%", filter="FID_*")

You can also filter type or add more specific filter condition after with if contition in for loop

for exemple get only editable fields

def fieldnames(dataset, sep=",", wild_card="*", fields_type="All"):

    return sep.join(list([f.name for f in arcpy.ListFields(
        dataset, wild_card, fields_type) if f.editable]))

In this case you can ignore information fields automaticaly set by system has:

  • FID OID OBJECTID
  • creator date_cretor updater date_update (if you have active tracking change in database)
  • GUID and GlobalID
-1
# Python Script
# List all columns from every FC within a dataset
# GIS 10.1 SP 1, FGDB
import arcpy
import os

arcpy.env.workspace = "C:\NBU_GIS_10_1\NBU_GIS_10_1_GDB\NBU_GIS_10_1_GDB.gdb"

datasets = arcpy.ListDatasets(feature_type='feature')
print datasets

for ds in datasets:
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds):
        path = os.path.join(arcpy.env.workspace, ds, fc)
        print path
        drive, path1, path2, GDB, dataset, fc = path.split('\\')
        print ("%(drive)s" % vars())
        print ("%(path1)s" % vars())
        print ("%(path2)s" % vars())
        print ("%(GDB)s" % vars())
        print ("%(dataset)s" % vars())
        print ("%(fc)s" % vars())
        path = drive + path1 + path2 + GDB + dataset + fc
        print path
        columns = drive + '\\' + path1 + '\\' + path2 + '\\' + GDB + '/' + dataset + '/' + fc
        print columns
        stuff = ("%(columns)s" % vars())
        fields = arcpy.ListFields("%(stuff)s" % vars())

        for field in fields:
            colmm = ("{0}".format(field.name))
            print ("%(colmm)s" % vars())
            columns = drive + '\\' + path1 + '\\' + path2 + '\\' + GDB + '/' + dataset + '/' + fc + '/' + ("%(colmm)s" % vars())
            print ("%(columns)s" % vars())
2
  • 3
    Highlight your code and press the { } button to format it correctly.
    – ianbroad
    May 12, 2015 at 16:34
  • This seems to be answering a different question.
    – PolyGeo
    May 12, 2016 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.