Add fields where field names are stored in a list - Python

1058
6
Jump to solution
09-19-2018 09:11 AM
by Anonymous User
Not applicable

Hello. 

I have been working on a tool that does a bunch of stuff, but the piece I am currently stuck on is getting the script to create fields where the field names are stored in a list, and the list is created using an already existing feature class. The idea is that the fields will be identical to the existing feature class in order to run the append tool. Here is what I have so far...I am having a hard time figuring out how to tell the script to create the fields based on the items in the fieldnames list using the types in the fieldtypes list.

#Get list of fields and parameters from original FC.
fieldnames = []
fieldtypes = []
fields = arcpy.ListFields(bound)
for field in fields:
    fieldnames.append(field.name)
for field in fields:
    fieldtypes.append(field.type)
fieldnames.remove("OBJECTID")
fieldnames.remove("SHAPE")
fieldtypes.remove("OID")
fieldtypes.remove("Geometry")
fieldtypes = ["TEXT" if x=="String" else x for x in fieldtypes]
fieldtypes = ["DOUBLE" if x=="Double" else x for x in fieldtypes]

#Add fields from list with field types.
for field in fieldnames:
    arcpy.AddField_management(newfc, field.name, field.type, "", "", "", "", "", "", "")  

Thanks!

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

The `fieldnames` list is a list of strings, so its items do not have properties `name` or `type`. You can access the proper items using the list indices:

for i in range(len(fieldnames)):
    arcpy.AddField_management(newfc, fieldnames[i], fieldtypes[i], "", "", "", "", "", "", "")  ‍‍

Better yet, make a dictionary and add both the field name and type to each item in order to concretely associate them.

View solution in original post

6 Replies
JoshuaBixby
MVP Esteemed Contributor

Why not use Create Feature Class—Data Management toolbox | ArcGIS Desktop  and use the template parameter?  From what you have said here, I think that would address your need and be simpler.

0 Kudos
by Anonymous User
Not applicable

The new feature class already exists, with geometry in it, but nothing else. So I am not really trying to create a new feature class to apply these fields to, it already exists, just in a simple form.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If the only field in an existing feature class is geometry and the feature class is empty, why not delete the feature class and re-create it using a template to have the schemas match rather than going through and enumerating all the fields of another feature class and manually creating each field.  Is there some reason you can't delete the existing feature class?

0 Kudos
by Anonymous User
Not applicable

Well, I want to keep that geometry to append to the feature class that the fields are being pulled from. The geometry is created by dissolving various shapes from another feature class. So the geometry is necessary to import into the current feature class.

0 Kudos
DarrenWiens2
MVP Honored Contributor

The `fieldnames` list is a list of strings, so its items do not have properties `name` or `type`. You can access the proper items using the list indices:

for i in range(len(fieldnames)):
    arcpy.AddField_management(newfc, fieldnames[i], fieldtypes[i], "", "", "", "", "", "", "")  ‍‍

Better yet, make a dictionary and add both the field name and type to each item in order to concretely associate them.

by Anonymous User
Not applicable

This appears to have worked perfectly.

Thanks!

0 Kudos