Is there a specific way to assign the object that for example row[0] returns in a SearchCursor
to a new field name? I mean how to write it in add field syntax, this doesn't work: arcpy.AddField_management(fc, str(row[0]), "DOUBLE")
. That field is a string field by the way. I can't find such an example anywhere online. Here is the code:
import arcpy
fc = "C:/Users/salahmed/Documents/ArcGIS/Test.shp"
myList = []
myList2 = []
myList3 = []
myField = ["USE"]
myFields = ["USE", "Stadteil", "PERCENT"]
with arcpy.da.SearchCursor(fc, myField) as cursor:
for row in cursor:
if row[0] in myList:
pass
else:
arcpy.AddField_management(fc, str(row[0]), "DOUBLE")
myList.append(row[0])
del cursor
-
What error message do you get? Are you trying to add a field inside the cursor? Could you show you code?GISGe– GISGe2016年06月30日 09:24:15 +00:00Commented Jun 30, 2016 at 9:24
-
Runtime error Traceback (most recent call last): File "<string>", line 17, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", line 3246, in AddField raise e ExecuteError: ERROR 999999: Error executing function. Failed to execute (AddField). This is the error. And yes I am adding field inside the cursor. I don't know how to share my code here in comments? Just copy paste?Salman– Salman2016年06月30日 09:29:04 +00:00Commented Jun 30, 2016 at 9:29
-
1You can edit your question to add the code. But you can't add a field inside a cursor.GISGe– GISGe2016年06月30日 09:32:27 +00:00Commented Jun 30, 2016 at 9:32
-
Oh, that I didn't know. But I wanted to loop through the rows, collect specific values from a field and add new fields with those names. Any other way?Salman– Salman2016年06月30日 09:34:24 +00:00Commented Jun 30, 2016 at 9:34
-
2Just loop through your list at the end rather than in the cursorJamesLeversha– JamesLeversha2016年06月30日 09:37:06 +00:00Commented Jun 30, 2016 at 9:37
1 Answer 1
As suggested by @JamesLeversha, create a list of the fields to be added then add those fields outside the cursor:
import arcpy
fc = "C:/Users/salahmed/Documents/ArcGIS/Test.shp"
myList = []
myList2 = []
myList3 = []
myField = ["USE"]
myFields = ["USE", "Stadteil", "PERCENT"]
with arcpy.da.SearchCursor(fc, myField) as cursor:
for row in cursor:
if row[0] in myList:
pass
else:
myList.append(row[0])
for f in myList:
arcpy.AddField_management(fc, f, "DOUBLE")
And by the way the del
statement is useless if you use with
to create your cursor.
-
Just wondering, I know the user hasn't mentioned it but should the user be concerned with if the name of the field is valid or not?JamesLeversha– JamesLeversha2016年06月30日 09:54:40 +00:00Commented Jun 30, 2016 at 9:54
-
The names are just normal strings. no invalid names there. And the above method works. Thank you.Salman– Salman2016年06月30日 09:56:45 +00:00Commented Jun 30, 2016 at 9:56
-
1@JamesLeversha - Sure you could make all sorts of checks - is the field name valid, not too long for a shapefile, does the field already exists in the table. In this case it doesn't seem to be necessary!GISGe– GISGe2016年06月30日 09:57:55 +00:00Commented Jun 30, 2016 at 9:57
-
@GISGe Do you mean I can use the cursor and row objects once again in the same code without using the del statement here?Salman– Salman2016年06月30日 09:58:02 +00:00Commented Jun 30, 2016 at 9:58
-
1@Salman if you want to use that section of code again later in the script then consider looking up creating a function tutorialspoint.com/python/python_functions.htmJamesLeversha– JamesLeversha2016年06月30日 10:02:48 +00:00Commented Jun 30, 2016 at 10:02