Strange behavior of getValue (field_name) method for Row object

1257
8
Jump to solution
10-14-2012 04:44 AM
MohammadIshfaq
New Contributor III
Greetings all,
I just wanted to check if I am the only one experiencing this problem or might be making a mistake in the usage of Row object 'getValue(field_name)' method. I believe this method was working fine with ArcGIS 10 release but it is throwing error in 10.1. I tried to go through the documentation at 'http://resources.arcgis.com/en/help/main/10.1/index.html#//018z0000008r000000' but it seems that code sample at same page never uses this method instead is using 'row.FIELD_NAME'. The row.FIELD_NAME method works fine but it is kind of hardcoding the field name which no one would like. Please comment or suggest if you have a solution to this problem. Here is the code snippet which throws an error.


cur = arcpy.SearchCursor(inputFC)
row = cur.Next()

while row:
        print(row.getValue(field_name)) # if I just replace this line with 'print(row.FIELD_NAME) then it works fine.
        row = cur.Next()

del cur

Thanks in advance for your time and support.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JillianPenney
Esri Contributor
Hi,

Have you tried using a for loop to retrieve the Global ID values? The following worked for me:

[HTML]
import arcpy

SC = arcpy.SearchCursor(r'C:\Data\TestData.gdb\Roads')
field_name = 'GlobalID'
for row in SC:
    print row.getValue(field_name)
del row, SC   

[/HTML]

View solution in original post

0 Kudos
8 Replies
T__WayneWhitley
Frequent Contributor
I believe you have to quote the field - for example if you actually have a field by the name, fieldname:
row.getValue("fieldname")

(unless of course fieldname is a variable representing text, as in:
fieldname = "fieldname"
0 Kudos
T__WayneWhitley
Frequent Contributor
I believe you have to quote the field - for example if you actually have a field by the name, fieldname:
row.getValue("fieldname")

(unless of course fieldname is a variable representing text, as in:
fieldname = "fieldname"



EDIT:  Also, I question (1) your use of the print statement and (2) if you have to convert the return value to string.  Such as:

print str(row.getValue("fieldname"))

I thought it an interesting additional note that some py functions return strings and may need to be converted to be useful -- e.g., apparently returning results on getCount is string and in the following sample online it is made an integer for further use (a little counterintuitive):

(see section Getting results from a tool)
http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000000n000000
0 Kudos
T__WayneWhitley
Frequent Contributor
EDIT:  Also, I question (1) your use of the print statement and (2) if you have to convert the return value to string.  Such as:

print str(row.getValue("fieldname"))

I thought it an interesting additional note that some py functions return strings and may need to be converted to be useful -- e.g., apparently returning results on getCount is string and in the following sample online it is made an integer for further use (a little counterintuitive):

(see section Getting results from a tool)
http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000000n000000



Oops, should have said getCount returns an object that is converted to string w/ getOutput and in the sample, the necessary part of the string (which is the count only), is converted to integer.  Sorry for any confusion.
0 Kudos
MohammadIshfaq
New Contributor III
Oops, should have said getCount returns an object that is converted to string w/ getOutput and in the sample, the necessary part of the string (which is the count only), is converted to integer.  Sorry for any confusion.


Thanks Wayne_Whitley for your informative answers. However, none of these suggestions worked for me. It is a bit strange as they changed the searching api a bit and now we have to code something like this.


try:

    fields = ["OBJECTID","AREA_ID"]

    with arcpy.da.SearchCursor(sourceObjectFilePathName, fields) as cursor:
        for row in cursor:
            print("{0},{1}".format(row[0], row[1])

    del cursor

except Exception as e:
    print e.message

Though, strange enough for me, I can't get the value of 'GLOBALID' field in a feature class as following exception is thrown when I add it to the search criteria. I wonder way and it is kind of really frustrating. I need it as it is the permanent unique key that I can use to compare features from one feature class to another.

************************************
unsupported field type GLOBALID #11
Exception Type is: <class 'Queue.Empty'>
************************************
0 Kudos
JillianPenney
Esri Contributor
Hi,

Have you tried using a for loop to retrieve the Global ID values? The following worked for me:

[HTML]
import arcpy

SC = arcpy.SearchCursor(r'C:\Data\TestData.gdb\Roads')
field_name = 'GlobalID'
for row in SC:
    print row.getValue(field_name)
del row, SC   

[/HTML]
0 Kudos
T__WayneWhitley
Frequent Contributor
Yes, well, looks like things are a little different at 10.1 and maybe it has something to do with a difference in the da module search cursor?  I am not using 10.1 yet, but trying to be more familiar before switching over.
Anyway, if you are trying to get at the OBJECTID, it looks like for a file gdb---

--- set up the cursor as such:
with arcpy.da.SearchCursor(fc, ("OID@")) as cursor:

--- of course get at the 'row' object as in "for row in cursor", then print the OID as such:
print("Current OID is {0}".format(row[0]))

Let me know if that works.


EDIT:  Looks like the OID@ token should work for any feature or table format supporting an Object ID field.
0 Kudos
MohammadIshfaq
New Contributor III
Thanks Wayne Whitley and JillPenney for your help. The problem is resolved with the Jill suggestion.

In my second reply I already have mentioned that I can retrieve objectID values but it won't serve my purpose because ObjectIDs are changed when ever Geodatabase administrator compresses the database and hence they are not unique. 

The for loop trick somehow worked and I could retrieve the GLOBALID information as well.

Once again thanks everyone for your support and time.
0 Kudos
T__WayneWhitley
Frequent Contributor
Yes, interesting, apparently there are 2 'versions' of the searchcursor with 10.1 -- I am not at 10.1 yet, but would be interested to know if this would work:

with arcpy.da.SearchCursor(fc, ("GLOBALID@")) as cursor:
     print("Current GLOBALID is {0}".format(row[0]))
0 Kudos