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
asked Jun 30, 2016 at 9:20
5
  • What error message do you get? Are you trying to add a field inside the cursor? Could you show you code? Commented 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? Commented Jun 30, 2016 at 9:29
  • 1
    You can edit your question to add the code. But you can't add a field inside a cursor. Commented 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? Commented Jun 30, 2016 at 9:34
  • 2
    Just loop through your list at the end rather than in the cursor Commented Jun 30, 2016 at 9:37

1 Answer 1

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.

answered Jun 30, 2016 at 9:43
7
  • 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? Commented Jun 30, 2016 at 9:54
  • The names are just normal strings. no invalid names there. And the above method works. Thank you. Commented 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! Commented 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? Commented 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.htm Commented Jun 30, 2016 at 10:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.