1

`I am looking to do the following task:

  • Look at the values in RegID_6k99 field (numerical field).
  • Select all records where "RegID_6k99 == 0". In this case 6 records should be selected.
  • For each of the selected records look at the FLABEL field value (String) for each record (6 different FLABELs).
  • Look at the field names of the feature class (fieldlist).
  • If any of the field names are equal to the value (string) of the FLABELs then sum the value (numerical) for each of the fields for that row. In this case for one row 6 fields have the same string as the FLABEL so for this row the six values will be summed into the new field (RegIntFlows) for that row.

In this case 6 rows will have a RegIntFlows field value.

  • Then increment for "RegID_6k99 == 1" and so on until the maximum RegID_6k99 value. All records should have a RegIntFlows value.

I have written some code but it doesn't run and needs modification.

Attached is screendump of feature class and current script.

import arcpy
fieldlist = arcpy.ListFields("C:/Temp/NIData.gdb/fc_f6000_in99Matx") #create list of fields
fc = "C:/Temp/NIData.gdb/fc_f6000_in99Matx" #reference feature class
result = [row[0] for row in arcpy.da.SearchCursor(fc, ("RegID_6k99"))] #search field
unique = set(result)
lengthDict = {}
for uni in unique:
    where = "\"RegID_6k99\" = '%s'" % (uni)
    lengths = [row[0] for row in arcpy.da.SearchCursor(fc, ("FLABEL"), where)]
    for field in fieldlist:
        if field.name.lower() == lengths: #if field name is equal to FLABEL value    
            lengthDict[uni] = sum(lengths)
            for each in lengthDict.keys():
                print "%s: %s" % (each, lengthDict[each])
                else #not sure how to finish

import arcpy
> fieldlist = arcpy.ListFields("C:/Temp/NIData.gdb/fc_f6000_in99Matx")
> fc = "C:/Temp/NIData.gdb/fc_f6000_in99Matx"
> rfield = "RegID_6k99"
> nfield = "FLABEL"
> # create an expression with proper delimiters
> expression = arcpy.AddFieldDelimiters(fc, rfield) + "=2"
> # create a search cursor with list comprehension to return a unique set of values based on sql expression
> values = [row[0] for row in arcpy.da.SearchCursor(fc, (rfield, nfield), where_clause=expression)]
> uniqueValues = set(values)
> list = []
> # check if FLABEL value equals field name
> for field in fieldlist:
    if field.name.lower() == values:
        list.append(field)
        RowRegFlows = sum(list)
        row.UpdateRow(row)


>>> ## Attempt to sum the values of the applicable field values

The code works - does what I want - up until the last three lines. I have attempted to do the last part of task in the last three lines but not confident about them. There is no error message (see attached jpg). If the FLABEL value for row is equal to all of the field names then I want to sum the values into a blank field (TotRegFlows) for each row. Do I need an update cursor as well? Can anyone advise? Many thanks Revised_code

closed as too broad by PolyGeo Jan 23 '15 at 4:54

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    Don't just say that it doesn't work. Tell us which line is the problem, & what error you get. – Devdatta Tengshe Jul 10 '14 at 10:02
  • 1
    Is the problem indentation? (the else statement needs to be at the same level as the if statement, not nested under for each...) – Erica Jul 10 '14 at 11:09
  • 1
    you are missing a : where the red is – user681 Jul 11 '14 at 22:37
  • 1
    @Lorraine Please consider breaking questions such as these up into simpler parts (and single questions). You will likely get much better answers that way. – Aaron Jul 12 '14 at 15:36
  • 1
    You need an update cursor. – ianbroad Jul 12 '14 at 17:07

Browse other questions tagged or ask your own question.