0

I have several files (shapefiles). Table 1 has only one row with several fields whereas Table 2 has several rows. What I want is to take a value from Table 1 and populate all rows in Table 2. I used a SearchCursor to find the value I want and an UpdateCursor to populate the rows.

import arcpy

arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split" 
#set workspace to folder with values

rows = arcpy.SearchCursor ("Table1.shp", "", "", "area_ha", "") 
#search for the value in attributetable

arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Split_wetlandt" 
#set workspace to folder with files which need to be updated

rows = UpdateCursor ("Table2.shp", "", "", "area_eco", "") 
#Update all rows of file with value from SearchCursor above

Running this, I keep getting this Errormessage:

Traceback (most recent call last):
File "<module1>", line 17, in <module>
NameError: name 'UpdateCursor' is not defined

If something is not defined, this means that Python sees it as a variable? Even if I could get rid of the errormessage: would it populate all the rows in Table 2 with the value or do I need to do something else? Note that I just started to learn Python and never scripted anything what so ever...

Edit 6/17/14 11:32 am

@ Aaron, would this replace the values in Table 2 with my searched values from table 1?

import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split" 
#set workspace to folder with values

rows = arcpy.SearchCursor (table 1", "", "", "area_ha", "") 
#search for the value in attributetable

arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Split_wetlandt" 
#set workspace to folder with files which need to be updated

with arcpy.UpdateCursor (Table 2 ["area_eco"]) 
#Update all rows of file with value from SearchCursor above
for row in cursor:
    area_eco.replace(SearchCursor)

EDIT 6/27614 12:04pm

I updated my script to this. Somehow it deleted my whole attributetable in Table 2 but I got it restored. I run it again and now nothing happens. It does not update the rows, the value in the rows stays 0.

import arcpy
arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split" 
#set workspace to folder with values

rows = arcpy.SearchCursor ("Table1", "", "", "area_ha", "") 
#search for the value in attributetable

arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Wetland" 
#set workspace to folder with files which need to be updated

rows = arcpy.UpdateCursor ("Table2", "", "", "area_ha", "")
del rows
  • 1
    You forgot arcpy.UpdateCursor() – Paul Jun 27 '14 at 14:23
3

You forgot to specify the arcpy module when calling UpdateCursor.

 rows = arcpy.UpdateCursor ("Table2.shp", "", "", "area_eco", "")

I second Aaron regarding using the with statement

Also, I would reference one of my OS answer regarding using either the "classic" cursor or the data access (da) one.

Finally, I would like to point you to a few resources to help you learn python/arcpy:

Hope it helps.

|improve this answer|||||
  • Updating this an trying to run the script, it bounces back with this RuntimeError: ERROR 999999: Error executing function. Cannot acquire a lock. Cannot acquire a lock. – Stophface Jun 27 '14 at 14:41
  • Close ArcMap/ArcCatalog and other Python IDEs that you have open. – ericchiasson Jun 27 '14 at 14:43
  • Weeeell, something happend, but not what I wanted it to do. It deleted now my whole attributestable instead of updating it... – Stophface Jun 27 '14 at 14:49
  • @Stophface I hope you have a backup... – ericchiasson Jun 27 '14 at 14:57
  • Sure I do :) Got it restored already. But where is the problem?! – Stophface Jun 27 '14 at 14:59
1

What is going on here is that the Python interpreter does not know where "UpdateCursor" came from. The correct syntax is as follows (i.e. ArcGIS 10.1+):

arcpy.da.UpdateCursor(YourFeatureclass, ["Field1", "Field2", "Field3", "etc"])

A very clean way to implement a cursor is by using the with statement:

with arcpy.da.UpdateCursor(YourFeatureclass, ["Field1", "Field2", "Field3", "etc"]) as cursor:
     for row in cursor:
          # Do something 

Cursor objects are automatically opened, processed in some way and closed automatically using a with statement. More details are available on the ESRI help section on UpdateCursors.

|improve this answer|||||
  • Hey. Thank you! I edited my post. I am not sure how I tell 'do something' to use the value of my search cursor – Stophface Jun 27 '14 at 14:36
  • 1
    @Stophface I would recommend opening a new question to address that. My initial thought is to run a search cursor and populate a list and then use that list to update a the new table via the update cursor. – Aaron Jun 27 '14 at 14:45
  • Ah, alright. Well, I tried the script I wrote with the added arcpy and it deleted my attributetable the shapefile I wanted to update. – Stophface Jun 27 '14 at 14:52
0

This is based on my understanding. I didn't test the script but this should work. Provide me a link to your data if you need further debugging.

import arcpy

arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Ecodis_clip_split" 
#set workspace to folder with values

# Create a cursor for Ecodis
cursorEco = arcpy.SearchCursor ("Table1", "", "", "area_ha", "") 
#search for the value in attributetable

arcpy.env.workspace = "C:\\Users\\Documents\\Scoring\\Wetland" 
#set workspace to folder with files which need to be updated

# Create a cursor for Wetland
cursorWet = arcpy.UpdateCursor ("Table2", "", "", "area_ha", "")

# Itereate each row within the Ecodis cursor 
for rowEco in cursorEco:
    print(rowEco.getValue("area_ha"))

    # Itereate each row within the Wetland cursor
    for rowWet in cursorWet:
        # Assign the value of Ecodis row to the Wetland row
        rowWet.setValue("area_ha", rowEco.getValue("area_ha"))
        # Save the changed made to the Weltand
        cursorWet.updateRow(rowWet)

# Clean-up
if cursorEco: 
    del cursorEco
if cursorWet:
    del cursorWet
if rowEco:
    del rowEco
if rowWet:
    del rowWet
|improve this answer|||||
  • Hey thanks a lot for your effort. SyntaxError: Cant assign to function call (<module3>, line 34) pops up highlighting this bit of code rowWet.setValue("area_ha") = rowEco.getValue("area_ha") I guess there is a mistake in the Syntax? I put already 4 spaces in there but that did not help. – Stophface Jun 27 '14 at 17:31
  • @Stophface I've made a change. See how it works. – ericchiasson Jun 27 '14 at 17:41
  • It would likely be more efficient if you accessed the value of row.Eco only once. Also, you can use a try...except block for your deletions. Less lines and it handles exceptions with lock files better. – Paul Jun 27 '14 at 17:45
  • Error executing function. The index passed was not within the valid range. Aaaand it delets all my rows in the attributetable where the value is supposed to go in again. – Stophface Jun 27 '14 at 18:11
  • Hey. I tried something different with a list and had some questions about that. The solution is here gis.stackexchange.com/questions/103515/… Thanks a lot for your effort :) – Stophface Jun 27 '14 at 18:21

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.