Python arcpy.da.SearchCursor error 'tuple' object has no attribute 'getValue'

5392
8
03-04-2016 05:47 AM
JoseSanchez
Occasional Contributor III

Hello everyone,

I am changing in my program the arcpy.SearchCursor  to arcpy.da.SearchCursor and when I run the code I am getting the following error message:

Python Error occurred

'tuple' object has no attribute 'getValue'

  # Get field objects from source FC AGREEMENT
  #
  dsc = arcpy.Describe(AGREEMENT)
  fields = dsc.fields
  #
  # List all field names except the OID field
  #
  fieldnamesYesterday = [field.name for field in fields if field.name != dsc.OIDFieldName]


  curYesterday = arcpy.da.SearchCursor(AGREEMENT, fieldnamesYesterday)



  for  rowYesterday in curYesterday:


        agreementID = rowYesterday.getValue("AGMID")
        datetimeVal = rowYesterday.getValue("DIGIDATE")
        print agreementID
        print datetimeVal


0 Kudos
8 Replies
DanPatterson_Retired
MVP Esteemed Contributor

indentation errors if that is what you have

Accessing data using cursors—Help | ArcGIS for Desktop

0 Kudos
RuthBowers
New Contributor III

This is a matter of simply reading the help files. The two different cursors use different syntax.

First read this:

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Then read this:

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

This will answer your question.

JoseSanchez
Occasional Contributor III

Hello everyone,

I am following this sample to list all the fields:

Using arcpy.da.InsertCursor to insert an entire row that is fetched from a search cursor? - Geograph...

What is wrong with my code, could you please be more specific.

Thanks

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi Jose,

You need to reference the fields using their list index.  For example, if AGMID is the 1st field, and DIGIDATE is the 3rd field, your code would be:

for rowYesterday in curYesterday:  
    agreementID = rowYesterday[0] 
    datetimeVal = rowYesterday[2]
    print agreementID  
    print datetimeVal
JoshuaBixby
MVP Esteemed Contributor

Are you following the code in the SE thread because that is what you are doing, i.e., inserting records from one dataset into another, or are you using the code just for its general structure?  If the former, it might be quicker to use either the Copy Features tool or Append tool.

0 Kudos
JoseSanchez
Occasional Contributor III

The samples from ESRI always show a hardcoded list of fields ['fieldA', 'fieldB']  but in my case I need to read all the fields from the feature class, and list them in the "field_names" parameter.

arcpy.da.SearchCursor(in_table, field_names, {where_clause}, {spatial_reference}, {explode_to_points}, {sql_clause})

0 Kudos
WesMiller
Regular Contributor III
0 Kudos
AndrewKeith3
Occasional Contributor

Here is the code you need:

# Get field objects from source FC AGREEMENT  
#  
dsc = arcpy.Describe(AGREEMENT)  
fields = dsc.fields  
#  
# List all field names except the OID field  
#  
fieldnamesYesterday = [field.name for field in fields if field.name != dsc.OIDFieldName]  

curYesterday = arcpy.da.SearchCursor(AGREEMENT, fieldnamesYesterday)  

for rowYesterday in curYesterday:  
    agreementID = rowYesterday[fieldnamesYesterday.index("AGMID")]  
    datetimeVal = rowYesterday[fieldnamesYesterday.index("DIGIDATE")]  
    print agreementID  
    print datetimeVal