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
Best Answer
You forgot to specify the arcpy module when calling UpdateCursor.
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.