Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm trying to add multiple fields to a table using arcpy and am wondering if it's possible to have multiple fields declared in the AddField management, and their values, separated by commas or something? Here is the code as stands, which is slightly cumbersome:

import arcpy, sys

arcpy.env.workspace = "C:\\gislab2\\Python\\take_home"
shapefile = "USCancer2000.shp"

#Set fieldname variables
field_name1 = "Crdrt"
field_name2 = "Crdrt_min"
field_name3 = "Crdrt_max"
field_name4 = "Ageadj"
field_name5 = "MinAgeAdj"
field_name6 = "MaxAgeAdj"

###Declare each standard population per age group, and for summed standard population
Pop1Stnd=78784000 
Pop2Stnd=150486000
Pop3Stnd=45364000
TotalStnd=274634000


    #Add the fields one at a time 
    arcpy.AddField_management(shapefile,field_name1,"DOUBLE",
"5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name2,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name3,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name4,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name5,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")
    arcpy.AddField_management(shapefile,field_name6,"DOUBLE", "5","4","","","NULLABLE","NON_REQUIRED","")

I know there's a way to create a loop to do it, but was wondering if there was a simpler and more direct way.

share|improve this question
1  
This sounds like a homework question, but I would say that adding your fields in a loop is the most efficient way to go assuming your field types are all the same. –  RyanDalton Apr 7 '13 at 19:00
    
self-edification, not homework. –  David Meek Apr 7 '13 at 22:42
    
The file paths from -all- of your posts suggest otherwise. –  Geoist Apr 7 '13 at 23:31
    
just because I'm in a class doesn't equate a question with homework. –  David Meek Apr 8 '13 at 0:02
add comment

2 Answers

A few techniques involved here. First off, I am storing all the arguments as tuples in a list. I can iterate through the list using a for-in loop. The tuples can be unpacked into function arguments using the * operator. I could have just gone with field names in a list, but then you would have not been able to be flexible in the other attributes of the field.

The shapefile, though, does stay constant throughout (and I assume that the name of the shapefile is stored in the variable shapefile as above). So, I do tuple concatenation inside the loop to build my entire set of arguments:

(shapefile,) + field

The trailing comma on shapefile, is necessary to tell the interpreter that you are creating a tuple with only one element. The end result is a tuple that can be unpacked with the * operator. As an example, the first tuple processed will be:

("USCancer2000.shp","Crdrt","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED","")

Notice that tuple concatenation takes precedence over the * operator, so that the entire concatenated tuple is unpacked instead of just (shapefile,).

fields = [
    ("Crdrt","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED",""),
    ("Crdrt_min","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED",""),
    ("Crdrt_max","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED",""),
    ("Ageadj","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED",""),
    ("MinAgeAdj","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED",""),
    ("MaxAgeAdj","DOUBLE","5","4","","","NULLABLE","NON_REQUIRED","")
]
for field in fields:
    arcpy.AddField_management(*(shapefile,) + field)
share|improve this answer
    
A really useful answer, was trying to achieve this with dictionary first, but then recalled the tuples and saw your answer :) –  Alex Tereshenkov Sep 10 '13 at 15:12
add comment

You may want to consider not using Python at all on this one but the advice here can also be used within a Python script.

If you want to avoid multiple AddFields consider creating a single empty table with all those fields and then using Join Fields based on the OID field.

It should join them all in one go.

You could create your empty Table manually by creating your new Table in the Catalog window or use Create Table/Add Field(s) in a Python script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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