How do I get list of fields from feature class/shapefile using tool?

4233
2
Jump to solution
05-15-2013 06:21 AM
IB1
by
Occasional Contributor
I'm making a custom tool that will allow the user to make many different changes to the fields of a chosen feature class or shapefile.

Part of my code is:

fc = arcpy.GetParameterAsText(0)


And then when I create the tool parameters I choose the Data Type as Data Element.

The next input I get from the user is the field from the fc that they want to use to copy data from into a new field.

copy_field = arcpy.GetParameterAsText(1)


For this parameter I've tried Field, Field Info and Field Mappings but none of them give me the fields of the inputted fc.

How can I link this parameter to the first one to give me a list of the fields within the fc?
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ChrisSnyder
Regular Contributor III
Do you want the user to select the field(s) they want to use? If so, the Data Type would be 'Field' with MultiValue = 'Yes' (to allow for multiple selections) and Obtained From would be set to your input feature class. I would recomend that you set your input layer to be a Data Type of 'Feature Layer', that way users can drag layers into the tool from the ArcMap TOC.

If you just want to list the fields in the input layer, it'd be something like this:

print ",".join([field.name for field in arcpy.ListFields(inputLayer)])

View solution in original post

0 Kudos
2 Replies
ChrisSnyder
Regular Contributor III
Do you want the user to select the field(s) they want to use? If so, the Data Type would be 'Field' with MultiValue = 'Yes' (to allow for multiple selections) and Obtained From would be set to your input feature class. I would recomend that you set your input layer to be a Data Type of 'Feature Layer', that way users can drag layers into the tool from the ArcMap TOC.

If you just want to list the fields in the input layer, it'd be something like this:

print ",".join([field.name for field in arcpy.ListFields(inputLayer)])
0 Kudos
ThaiTruong
Occasional Contributor II
To list all the fields from your input featureclass, use arcpy.Listfields

Try to debug the following code, you'll get a print statement with the name, alias, type, and length for each field from your input fc:

fieldList = arcpy.ListFields(fc)
for field in fieldList:
    fname = field.name
    falias = field.aliasName
    ftype = field.type
    flength = field.length
    print "%s \t %s \t %s \t %i" % (field.name, field.aliasName, field.type, field.length)