1

I fear I am missing something fundamental.

I can get a list of fields from one layer using ListFields, and look at the properties:

fields = arcpy.ListFields(feature_class)

# Iterate through the list of fields
for field in fields:
    # Print field properties
    print("Field:       {0}".format(field.name))
    print("Alias:       {0}".format(field.aliasName))
    print("Type:        {0}".format(field.type))
    print("Is Editable: {0}".format(field.editable))
    print("Required:    {0}".format(field.required))
    print("Scale:       {0}".format(field.scale))
    print("Precision:   {0}".format(field.precision))

Is there an easy way to add these fields with the exact same properties to a new layer using AddField_management?

I was hoping I could pass the field object directly and not have to decompose it into the individual properties. But the following does NOT work:

for field in fields:
    # add fields with the same properties
    arcpy.AddField_managment(feature_class2,field)

It does not seem very efficient to break out and pass every field property (field.name, field.type, etc). Surely there is a better way.

I get the error:

ERROR 000623: Invalid value type for parameter field_name

  • You seem to have created two accounts. Please use these instructions to merge them ASAP. That will prevent a frustrating editing experience for both you and anybody trying to help you. gis.stackexchange.com/help/merging-accounts – PolyGeo Dec 30 '16 at 1:41
  • Are you trying to transfer the fields to an existing layer (that doesn't have the fields), or to a new blank feature class? – phloem Dec 30 '16 at 21:01
1

My solution was to use a function to create a dictionary, then unpack the dictionary in the addField function. For example:

def fieldInfoUnpack(fieldInfo,name,alias):
    return  {"field_name":name,
    "field_type":fieldInfo.type,
    "field_precision":fieldInfo.precision,
    "field_scale":fieldInfo.scale,
    "field_length":fieldInfo.length,
    "field_alias":alias,
    "field_is_nullable":fieldInfo.isNullable,
    "field_is_required":fieldInfo.required,
    "field_domain":fieldInfo.domain}
arcpy.AddField_management("roadwaysplts",**fieldInfoUnpack(fieldInfo,"newfld","fld"))
0

I think you need to do something more like this:

for field in fields:
    # add fields with the same properties
    arcpy.AddField_management(feature_class2,field.name)

but with more field properties being used as parameters.

The error message tells you that using the field object rather than accessing its properties is not an option.

If you were able to use a field object as a parameter with Add Field then that would appear in the Data Type column of the Syntax section of its Help (which it does not).

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.