[GIS] Add multiple fields in attribute table

arcpyattribute-tablepython

How do I add in multiple fields in the attribute table? In the attribute table I have crop types such as corn, cotton, sorghum….etc (as shown image below), in square meters. I want to generate new fields so I can calculate into acres. I am using python that would have add fields, but I would still have to provide field names, one by one (and there are a lot of field names to enter). Can I just have GIS to generate fields that are based on the existing fields without me listing them?

Image of table is attached.

enter image description here

Best Answer

You'll have to loop through your current fields with ListFields. Then in your AddField_management() call just add '_acres' or something that identifies it as different from your current field name.

Something along these lines:

for field in arcpy.ListFields(featureClass):
    arcpy.AddField_management(featureClass, field.name + '_acres', 'FLOAT')

If you don't want to add a new field for every field in your feature class, you'll have to add some simple conditional statements:

for field in arcpy.ListFields(featureClass):
    if field.name == 'badField': # Name of field you don't want to add acreage for
        print "Not going to add this field"
    else:
        arcpy.AddField_management(featureClass, field.name + '_acres', 'FLOAT')