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

2
  • 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
    Commented Dec 30, 2016 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
    Commented Dec 30, 2016 at 21:01

3 Answers 3

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
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).

0
def batch_addfields(str_path_obj_out, str_path_obj_add, lst_str_fld_add):
    # str_path_obj_out      : absolute path to the feature class or table where the fields go
    # str_path_obj_add      : absolute path to the feature class or table from the fields come
    # lst_str_flds_add      : list of fields to add
    # assumes all field names are free in obj_out (OBJECTID should be rename)

    # Now this beatiful piece of idiomatic Python will (braggs) copy all properties
    # of n desired fields listed in lst_str_flds_add from str_path_obj_add into str_path_obj_out

    [arcpy.AddField_management(in_table = str_path_obj_out,
                                field_name = fld.name,
                                field_type = fld.type,
                                field_precision = fld.precision,
                                field_scale = fld.scale,
                                field_length = fld.length,
                                field_alias = fld.aliasName,
                                field_is_nullable = fld.isNullable,
                                field_is_required = fld.required,
                                field_domain = fld.domain
                                ) 
             for fld in arcpy.ListFields(str_path_obj_add)
             if fld.name in lst_str_fld_add]
2
  • Welcome to GIS.SE! Could you please provide some more explanation for your code. Simply pasting code does not help people who are revisiting the question with understanding the solution.
    – root676
    Commented Aug 28, 2019 at 11:22
  • Ok thanks for bot because thought about that but your comment convinced me to do it, Regards. Commented Aug 28, 2019 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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