0

I have a table in a geodatabase: tableA

A column in that table lists a couple of feature classes that are in the geodatabase.

I want to make a loop on each name, count the number of rows in the attribute table of each feature class and store this value in a column of 'tableA'.

Here is what I've tried:

arcpy.env.workspace =r'C:\Users\user\Desktop\geodatabase\geoA.gdb'
dataset = arcpy.ListTables("tableA") #the table needed is 'tableA'
arcpy.AddField_management("tableA", "rows_count", "LONG") #create field with the count_rows
for i in dataset:
    with arcpy.da.UpdateCursor("tableA",["file_name","rows_count"]) as cur: #the fields that needed
        for row in cur:
            try:
                nrow = arcpy.GetCount_management (row[0]) #count each layer's rows
                row[1].setValue('rows_count', nrow) #fill the column with the values found
                cur.updateRow(row) #apply changes        
            except arcpy.ExecuteError as e:                     
                print(e)

AttributeError: 'NoneType' object has no attribute 'setValue'

In the result, the rows_count is null for all rows at the creation of the field.

What can be done here?

How the table looks:

file_name  rows_count
file1       null
file2       null
file3 ...   null

Question:

In the main table, how to count the number of rows for each of these files and add store this vlue in a column ?

UPDATE

Wanted output

tableA

file_name  rows_count
file1         0
file2         12
file3         1
...

Where file1,file2, etc are actual feture classes in the gdb and rows_count are the number of rows in each file.

  • You are trying to perform GetCount() on one particular field ("filename") in a single row in the table rather than the whole table. The input should be a layer or feature class. desktop.arcgis.com/en/arcmap/latest/tools/… – Matt Needle Nov 15 '18 at 10:47
  • each row exists as a feature layer as well in the same gdb – user131811 Nov 15 '18 at 10:49
  • I don't have access to ArcGIS right now but I think you should be calling GetCount() on i rather than the indexed field in your cursor (row[0]). – Matt Needle Nov 15 '18 at 10:51
  • the counting works ok.So this doesn't have to change. The problem is to put the numbers in the new column. – user131811 Nov 15 '18 at 10:56
  • Is the field being successfully added to "TableA"? The UpdateCursor has no reference to it. – Matt Needle Nov 15 '18 at 11:11
2

Your error comes from the fact that arcpy.da returns a cursor with no method setValue. It returns a list at each iteration, that you can manipulate like a list. 'row' will contain the values of the fields selected in the cursor inside a list. Just use indice to read and write those field values (row[index] = new_value_for_the_field_at_the_specified_index).

the use of GetCount() should also be modified, because GetCount doesn't return a number but it returns an object that contains a string (don't ask me why).

and the loop on dataset is useless because you have a single table to fill.

arcpy.env.workspace =r'C:\Users\user\Desktop\geodatabase\geoA.gdb'
arcpy.AddField_management("tableA", "rows_count", "LONG") #create field with the count_rows
with arcpy.da.UpdateCursor("tableA",["file_name","rows_count"]) as cur: #the fields that needed
    for row in cur: #for each row
        try:
            #GetCount will return a "result object", the count itself is the first element of the result of getCount() 
            row[1] = int(arcpy.GetCount_management(row[0])[0]) #reads the name of feature class (check that paths are correct)
            cur.updateRow(row) #apply changes        
        except arcpy.ExecuteError as e:                     
            print(e)
  • I have updated the question. – user131811 Nov 15 '18 at 13:10
  • it now says: TypeError: 'tuple' object does not support item assignment – user131811 Nov 15 '18 at 13:35
  • The dataset loop is indeed useless but what is the way to actually populate the field? – user131811 Nov 15 '18 at 13:44
  • What is your ArcMap version ? – radouxju Nov 15 '18 at 13:48
  • 10.6 is the version – user131811 Nov 15 '18 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.