Select to view content in your preferred language

SetValue help?

1606
2
Jump to solution
10-04-2012 12:06 PM
RichardThurau
Deactivated User
Hi All,
Trying to run a basic field update, but getting error.
Code:

ClassList=["Tree_Canopy", "Impervious", "Vegetation", "Soil", "Water"]  AcEvalRas=(os.path.join(DirAc, "AcEvalRas_g"+str(i)+"i")) rows = arcpy.UpdateCursor(AcEvalRas) for row in rows:     if row.Value == 1:         row.SetValue(row.Class, ClassList[0])         row.SetValue(row.Ref, ClassList[0])         rows.updateRow(row)     elif row.Value == 2:         row.Class = row.getValue(ClassList[1])         row.Ref = row.getValue(ClassList[1])         rows.updateRow(row)     elif row.Value == 3:         row.Class = row.getValue(ClassList[2])         row.Ref = row.getValue(ClassList[2])         rows.updateRow(row)     elif row.Value == 4:         row.Class = row.getValue(ClassList[3])         row.Ref = row.getValue(ClassList[3])         rows.updateRow(row)


Error Code:
Traceback (most recent call last):
  File "X:\\TV_LC_2.4_Accuracy2_overlay.py", line 66, in <module>
    row.SetValue(row.Class, ClassList[0])
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 28, in __getattr__
    raise AttributeError("%s" % attr)
AttributeError: SetValue

Line 66 is the first "SetValue" line.


I have tried several different versions, where I use setValue and getValue. I even tried a setValue(row.Class, row.getValue(ClassList[0])). The first elif line seems the cleanest to me, but that gives a 99999 Failure to Execute error. From the error messages, I think the first SetValue from the code above is the best so far.

Does the syntax look okay? Any ideas would be great.

This is a raster dataset within a GDB. Fields "Class" and "Ref" are both text with default precision.

Thanks

Rich
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Alum
Your AttributeError is from the case sensitive name being wrong. So, start by spelling setValue correctly.

>> type(row.setValue) <type 'instancemethod'> >>> type(row.SetValue) Traceback (most recent call last):   File "E:\Program Files (x86)\Wing IDE 3.2\src\debug\tserver\_sandbox.py", line 1, in <module>     # Used internally for debug sandbox under external interpreter   File "D:\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 28, in __getattr__     raise AttributeError("%s" % attr) AttributeError: SetValue >>> 


(It wasn't in arcgisscripting, but it is in arcpy, which is fully Pythonized.)

If you're operating on a raster attribute table, it's best to MakeTableView and than open the table view instead of trying to open the raster as a table.

Dictionaries are totally the way to go with simple lookups like this:

Lookups =[1,2,3,4] ClassList=["Tree_Canopy", "Impervious", "Vegetation", "Soil", "Water"] lookup = dict(zip(Lookups,ClassList)) ... for row in rows:     v = lookup[row.getValue("Value")]     row.setValue("Class", v)     row.setValue("Ref", v)     rows.updateRow(row)


Also, note the syntax of getValue and setValue - the first argument is a field name string ("Value"), not a field value (row.Value).

Hope this helps.

View solution in original post

0 Kudos
2 Replies
curtvprice
MVP Alum
Your AttributeError is from the case sensitive name being wrong. So, start by spelling setValue correctly.

>> type(row.setValue) <type 'instancemethod'> >>> type(row.SetValue) Traceback (most recent call last):   File "E:\Program Files (x86)\Wing IDE 3.2\src\debug\tserver\_sandbox.py", line 1, in <module>     # Used internally for debug sandbox under external interpreter   File "D:\ArcGIS\Desktop10.1\arcpy\arcpy\arcobjects\_base.py", line 28, in __getattr__     raise AttributeError("%s" % attr) AttributeError: SetValue >>> 


(It wasn't in arcgisscripting, but it is in arcpy, which is fully Pythonized.)

If you're operating on a raster attribute table, it's best to MakeTableView and than open the table view instead of trying to open the raster as a table.

Dictionaries are totally the way to go with simple lookups like this:

Lookups =[1,2,3,4] ClassList=["Tree_Canopy", "Impervious", "Vegetation", "Soil", "Water"] lookup = dict(zip(Lookups,ClassList)) ... for row in rows:     v = lookup[row.getValue("Value")]     row.setValue("Class", v)     row.setValue("Ref", v)     rows.updateRow(row)


Also, note the syntax of getValue and setValue - the first argument is a field name string ("Value"), not a field value (row.Value).

Hope this helps.
0 Kudos
RichardThurau
Deactivated User
My stumbling blocks always result in learning something, but this post was one of the most useful for all my day to day tasks.

Curtis, thank you so much. Worked great, and I understand what's happening a lot more.

Rich
0 Kudos