Select to view content in your preferred language

Loop through Columns and Rows

4735
8
Jump to solution
04-19-2012 10:45 AM
RichardThurau
Deactivated User
Hi,
I am trying to use an update cursor to loop through all fields and all rows and replace 0 with 9999. I've looked up a column command that appears to work for some people, but is not going well for me.
Here's the code:

import arcpy, os from arcpy.sa import * from arcpy import env arcpy.CheckOutExtension("Spatial") arcpy.env.overwriteOutput = True  areaTab1 = r"X:\DATA\ROW_SP_areaTab"  rows = arcpy.UpdateCursor(areaTab1) for row in rows:     for col in rows:         if row.getValue(row) == 0:             row.setValue(9999)             row.updateRow(row)


Error:
Traceback (most recent call last):
  File "C:\_Rich\Project_Temp\col_loop_test.py", line 13, in <module>
    if row.getValue(row) == 0:
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\arcobjects.py", line 945, in getValue
    return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.

This would be awesome and incredibly useful to be able to run instead of having to search each individual field.

Any help would be greatly appreciated!!

Thanks

Rich
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Oh thats a typo

rows = arcpy.UpdateCursor(areaTab1) cols = arcpy.ListFields(areaTab1) for row in rows:     for col in cols:         if row.getValue(col.name) == 0:             row.setValue(col.name, 9999)             rows.updateRow(row)

View solution in original post

0 Kudos
8 Replies
BruceNielsen
Frequent Contributor
I think you want:
if row.getValue(col) == 0:
0 Kudos
RichardThurau
Deactivated User
Thanks Bruce,

No dice. Same error. I also tried row.col and col.row, which I'm sure to the programmers out there, obviously would not work.

Here's some info about how to do it on StackOverflow:
http://stackoverflow.com/questions/2457193/looping-through-columns-in-a-csv-files-in-python


rt
0 Kudos
DarrenWiens2
MVP Alum
I'm not sure what the performance implications to this would be, but you might just abandon the UpdateCursor idea and run Calculate Field for each field. This is untested, but I think it should work.
import arcpy
inFeatures = r"X:\DATA\ROW_SP_areaTab 
fieldList = arcpy.ListFields(inFeatures)

for field in fieldList:
    codeblock = "def func(field):\n    if field == 0:\n        return 9999"
    expression = "func(!" + field.name +"!)"
    arcpy.CalculateField_management(inFeatures, field, expression,"PYTHON",codeblock)

Alternatively, for your code you're looping through the rows twice, not the columns at all.
import arcpy, os
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

areaTab1 = r"X:\DATA\ROW_SP_areaTab"

rows = arcpy.UpdateCursor(areaTab1)
cols = arcpy.ListFields(areaTab1)
for row in rows:
    for col in cols:
    if row.getValue(col.name) == 0:
        row.setValue(col.name, 9999)
        row.updateRow(row)

edit: changed some "col" to "col.name"
0 Kudos
MathewCoyle
Honored Contributor

Alternatively, for your code you're looping through the rows twice, not the columns at all.
import arcpy, os
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

areaTab1 = r"X:\DATA\ROW_SP_areaTab"

rows = arcpy.UpdateCursor(areaTab1)
cols = arcpy.ListFields(areaTab1)
for row in rows:
    for col in cols:
    if row.getValue(col.name) == 0:
        row.setValue(col.name, 9999)
        row.updateRow(row)

edit: changed some "col" to "col.name"


You'll want to indent under the field list loop as well.
for row in rows:
    for col in cols:
        if row.getValue(col.name) == 0:
            row.setValue(col.name, 9999)
            row.updateRow(row
0 Kudos
RichardThurau
Deactivated User
Mathew and Darren, Thanks for your replies. This is making sense to me.

I push this code:

rows = arcpy.UpdateCursor(areaTab1)
cols = arcpy.ListFields(areaTab1)
for row in rows:
    for col in cols:
        if row.getValue(col.name) == 0:
            row.setValue(col.name, 9999)
            row.updateRow(row)


And now get this error:
Traceback (most recent call last):
  File "C:\_Rich\Project_Temp\col_loop_test.py", line 35, in <module>
    row.updateRow(row)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 25, in __getattr__
    x = getattr(self._arc_object, attr)
RuntimeError: Row: Field updateRow does not exist

The key being this last line where Python is looking for a field called "updateRow", which of course is not a field name. Seems like the syntax is good up to the last line, but why is it not recognizing the update command?
0 Kudos
RichardThurau
Deactivated User
P.S. Darren, I don't want to use calculate field because the real purpose of this code will be to replace Null values with zeros and I know, a couple service packs ago at least, field calculator has a hard time with Null values from Python.

rt
0 Kudos
MathewCoyle
Honored Contributor
Oh thats a typo

rows = arcpy.UpdateCursor(areaTab1) cols = arcpy.ListFields(areaTab1) for row in rows:     for col in cols:         if row.getValue(col.name) == 0:             row.setValue(col.name, 9999)             rows.updateRow(row)
0 Kudos
RichardThurau
Deactivated User
Frikin Beautiful!

Sorry for overlooking the rows., I tried not to miss any of that silly stuff but guess I failed.

Thanks to you both for your time and attention.

Rich
0 Kudos