2

I am working with a point shapefile, for which I have created a new field NEW_ID. This represents a unique code for each point. This is more convenient to me than using the OBJECTID.

I want to fill the new field with the values coming from the following list: myvalues=range(1000000,1002332)

And I am trying to get this working, but the field is still <Null> after the snippet has run:

myFeatureClass=r'C:\temp\testGDB\test_shp'
cur = arcpy.UpdateCursor(myFeatureClass)

for row in cur:
    row.setValue('NEW_ID', row.getValue(myvalues[row]))
    cur.updateRow(row)

I suspect I cannot use row as an iterator. So, what should I use instead?


ArcGIS version: 10.3.1


  • What is the exact error including line number that you get when you run the precise code snippet that you have presented? What version of ArcGIS are you using? I ask because if you are using 10.1 or later I would recommend forgetting about the old style cursors and switch to ArcPy.da. – PolyGeo Jan 23 '17 at 11:30
  • 1
    Yes, row is an object, not a radix, but you really ought to start over from scratch usimg arcpy.da.UpdateCursor which uses a different syntax structure (and is 8-20 times faster). The new row will be an array of column values, so you still need to create and increment a radix value. I expect there are dozens of questions very similar here by now. – Vince Jan 23 '17 at 11:41
  • 1
    A shapefile cannot be within a geodatabase container -- they are two completely different formats. – Vince Jan 23 '17 at 11:45
  • 2
    To anonymize a feature class I always copy it to C:\temp\testGDB\testFC and then write my code snippet against that. – PolyGeo Jan 23 '17 at 11:50
  • 1
    Making a reproducible snippet is the "service charge" associated with getting free help from GIS SE. Please put in an effort to make standalone code (it might even solve the problem before you post it). – Vince Jan 23 '17 at 11:51
2

The problem is how you get your new ID number from your list myvalues. You are using your whole row object as an index which clearly makes no sense. As @PolyGeo suggests you would do best to use the newer cursors from the da module, they are far faster. But you can use the old ones too.

I assume you have created your list with as many rows as there are in the featureclass, otherwise you would run out of numbers!

If this was me I would have done it this way:

myFeatureClass=r'C:\Myfolder\target_shp'
cur = arcpy.UpdateCursor(myFeatureClass)

newid = 1000000
for row in cur:
    row.setValue('NEW_ID', newid)
    cur.updateRow(row)
    newid = newid + 1
  • This one I like. But the problem arises when you have an alphanumeric code to assign, instead of plain numbers. Something along the lines of AA01, for instance. – FaCoffee Jan 23 '17 at 11:38
  • 3
    Well you didn't say that! :) – Hornbydd Jan 23 '17 at 11:38
  • Yes, I didn't... I thought a general solution involving parsing the list could do for any situation. My bad. – FaCoffee Jan 23 '17 at 11:39
  • 1
    Well if the code is constant it would simply be: row.setValue('NEW_ID', "AA" + str(newid)) – Hornbydd Jan 23 '17 at 11:40

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.