Get Field Value

41474
8
07-25-2012 01:04 AM
yanaybrook1
New Contributor
hello,
Im trying to find/write a simple code line that will get the value from a
specific row and columm (field name). - I only have one table.
I cant use the modelbuilder tool (GetFieldValue) because i need it in python..

thx!
Tags (2)
8 Replies
ChristopherThompson
Occasional Contributor III
Look into Search Cursors (arcpy.SearchCursor or the new arcpy.da.SearchCursor if you're at 10.1).  These allow you to create an object containing the rows of a given table (spatial or otherwise) and step through that object row by row.  If you have a specific row or set of values in a feature class you can create a search cursor based on a sql query.

For example, if you had a feature class with a classification of things in a field named 'class' and you needed to extract all the values held in a field named 'Data' for records with a class = 1 you might do something like this:

things = arcpy.SearchCursor(yourFeatureClass, """ 'class' = "1" """,'','Data')
for thing in things:
   mydata = thing.Data
   ...code to do something with the mydata value, such as writing it to a text file, etc.. 


There may be other ways to approach this if you're looking for one value from a single row, but usually we want to deal with many rows in the same way, perhaps conditionally based on the the value held in a field.  Hope this helps.
DeeptiPuri
New Contributor
Hi There

I want to read a value from one column (result of calculate UTM tool) and it is same for all the points (most likely there will be only one point). I can use searchcursor in arcpy. But is there any simple way similar to GetFieldValue for arcpy?
Thanks
0 Kudos
JillianPenney
Esri Contributor
This sample code may help if you want all the values in a column, or if you want a specific value using getValue in a search cursor.

[HTML]
import arcpy

SC = arcpy.SearchCursor(r'C:\Data\TestData.gdb\Roads')
field_name = 'Object ID'
for row in SC:
    # if you want all values in the field
    print row.getValue(field_name)

    # you can also get specific values
    if row.getValue(field_name) == 5
        x = row.getValue(field_name)
        # using break allows you to stop the search cursor
        break

del row, SC
print x

[/HTML]
0 Kudos
RobertPotter
New Contributor II
Hi,

I am having this issue that the row.getValue(field.name) breaks my script

rows = arcpy.SearchCursor(DBase_file)    
for row in rows: 
    for field in desc.fields:
            rVal = row.getValue(field.name)
            arcpy.AddMessage("Field Name: " + field.Name + "Value: " + rVal)


In the examples above you have a haed coded field name but I am looping through unknown field names and it just stops at the rVal=... line.

Any ideas??
0 Kudos
T__WayneWhitley
Frequent Contributor
I question your use of desc.fields...
Not sure that describe object is iterable.  Can't you use the listFields function instead?  Careful with case, could be ListFields, look up the topic of listing data.
0 Kudos
ArkadiuszMatoszka
Occasional Contributor II
I question your use of desc.fields...
Not sure that describe object is iterable.  Can't you use the listFields function instead?  Careful with case, could be ListFields, look up the topic of listing data.


Hi,
As Wayne told, use  arcpy.ListFields instead of desc.fields, also you should change AddMessage statement a bit, to make sure it won't crush if field type is different than TEXT:

fields = arcpy.ListFields(DBase_file)
rows = arcpy.SearchCursor(DBase_file)    
for row in rows: 
    for field in fields:
            rVal = row.getValue(field)
            arcpy.AddMessage("Field Name: " + field + "Value: " + str(rVal)) # is not always string


Regards
Arek
0 Kudos
ZdeněkSoldán
Occasional Contributor

Hello,

I have a similar problem. I have a searchCursor that goes through a specific column in a table. On every row I need to compare the value from this row with the value on the next row and if they are equal do something and if they aren't equal do something else.

mxd = arcpy.mapping.MapDocument("CURRENT")
table = arcpy.mapping.ListTableViews(mxd)[0]
column = "name"
cursor=arcpy.SearchCursor(table)
for row in cursor:
    print(row.getValue(column))

I have this sample of code. It goes through all rows. But how can I compare the value from row[row] with the value from row[row+1]??

Thanks for any help.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I see you started a new question, which I would have suggested too.  For others...

Compare value from n and n+1 row in searchCursor

0 Kudos