1

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 Jun 30 '16 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 Jun 30 '16 at 9:29
  • 1
    You can edit your question to add the code. But you can't add a field inside a cursor. – GISGe Jun 30 '16 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 Jun 30 '16 at 9:34
  • 2
    Just loop through your list at the end rather than in the cursor – JamesLeversha Jun 30 '16 at 9:37
3

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 Jun 30 '16 at 9:54
  • The names are just normal strings. no invalid names there. And the above method works. Thank you. – Salman Jun 30 '16 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 Jun 30 '16 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 Jun 30 '16 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.htm – JamesLeversha Jun 30 '16 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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