Friday 2 June 2017

python - Possible to add multiple fields in single arcpy statement?


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.




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...