Add Fields

522
3
05-30-2013 11:38 PM
NilsLandmeyer
New Contributor
Hello,

is it possible with a phyton code to add more than 1 Field to a Table?
I have to calculate a few Fields in the Modelbuilder and would like to add these new Fields at once.

Best regards,

nils-demian
Tags (2)
0 Kudos
3 Replies
MarcEngelsma1
New Contributor II
Nils,
It's possible but I'm not sure it's faster or easier than adding a few more tools in modelbuilder.

I'd put the field names in a list and then you can loop the AddField command through that list. I'm not yet sure how you can handle different datatypes for the fields though. With all fields of the TEXT type it would look like this

Shapefile = r"C:\xxx\xxx\your shapefile here.shp"
FieldList = ["Field1", "Field2", "Field3", "etc"]

for Column in FieldList:
    arcpy.AddField_management(Shapefile, Column, "TEXT")


You can add more parameters after the field type to determine the field length, precision, etc
Hope this is helpful.

Marc
0 Kudos
NilsLandmeyer
New Contributor
Thanks Marc !

best regards

nils-demian
0 Kudos
TimDine
Occasional Contributor II
It isn't going to save you many characters of code, but if you wanted to expand your array idea make it a multi dimensional array or an array of objects.  Instead of a string for a field name you have an array, the items in the array could be the parameters for your add field.

Shapefile = r"C:\xxx\xxx\your shapefile here.shp"
FieldList = [["Field1","TEXT"], ["Field2","DOUBLE"]]

for Column in FieldList:
    arcpy.AddField_management(Shapefile, Column[0], Column[1])
0 Kudos