AddField_management question

584
2
03-20-2012 05:41 AM
deleted-user-rQoEFM5qzbHE
New Contributor II
I need to add multiple fields based on a numerical value and would like to set this up in a for loop based on the numerical value. I have tried in the past to concatenate a string with a number, but have had no luck. As a work around, I had to add in multiple lines of code to add fields (enough to ensure that I add enough fields). As an example, this is what I would like to do:

max = 20

for i = 1 to max:
  arcpy.AddField_management(InFC, "Field_" + str(i), ...)

Something to this effect. I have tried a couple of different ways to do this in the past, but haven't had any success. Is this possible?

Thanks,

Jeremy
Tags (2)
0 Kudos
2 Replies
RaphaelR
Occasional Contributor II
Hi Jeremy,

something like this should work:

import arcpy

max = 5
infile = r"e:\test\test.shp"

for i in range(1,max+1):  
    
    fname = "Field_"+str(i)
    arcpy.AddField_management(infile, fname, "DOUBLE") #starts with "Field_1", ends with "Field_5"
0 Kudos
deleted-user-rQoEFM5qzbHE
New Contributor II
Yeah, after talking with a co-worker this solution just popped into my head. I haven't tried it out yet, but it makes sense.

Thanks,

Jeremy
0 Kudos