8 Replies Latest reply on Jan 25, 2012 8:31 AM by mdenil

    Python script help for row.SetValue() function

    fjscott
      I am creating a python script and have a question about the row.SetValue() function. I have read in a table and a feature class along with a string value that represents one of the fields in the feature class. I join the table to the feature class and perform some calculations. Based on the result from those calculations, I then need to set the value of the of the field denoted by the string value. I am not sure what the proper code (if this is even possible) to do this. Here is what I have tried.

      First, I tried to set the 'SchoolLevel' field to a value:
      [INDENT]row.SetValue(SchoolLevel, M[0][1])[/INDENT]
      I am pretty sure that this doesn't work because there is a join.

      So, then I tried to add in the name of the feature class to the code as such:
      [INDENT]row.SetValue(Scenario_Template_1011.SchoolLevel, M[0][1])[/INDENT]

      This didn't work, so then I tried to put the feature class name in quotes:
      [INDENT]row.SetValue("Scenario_Template_1011."SchoolLevel, M[0][1][/INDENT]
      This throws me an invalid syntax error before I can even run the script. This makes sens, but I am not sure how to correctly code this up.

      Basically, Scenario_Template_1011 is the name of the feature class that is joined by another table and SchoolLevel is a variable representing a field in Scenario_Template_1011. I am trying to set the value of that field using a cell in a matrix that I created.

      Thanks in advance for any help!
        • Re: Python script help for row.SetValue() function
          rkallstrom
          Don't know much about Python or how joins work, but can you adjust the UpdateCursor to include fewer fields (4th item in argument)?

          Otherwise, next thing I would try would be a combination of the SearchCursor and UpdateCursor, though I don't know how these run concurrently.
          • Re: Python script help for row.SetValue() function
            snoop
            What are M[0][1]?  are these values from a python list? if so try,


            row.SetValue(SchoolLevel, M[0] + M[1])
            • Re: Python script help for row.SetValue() function
              fjscott
              Thanks for the responses. I think I know what my issue is but I have run into other problems and have to fix those before I know for sure. Ah, the joys of coding :).

              Snoop, M is an nxm matrix. In the example I am accessing the value of the cell in the first column, second row.
              • Re: Python script help for row.SetValue() function
                fjscott
                I have been unable to figure out why this is not working. Here is the code in the hopes that someone will be able to see where I am going wrong.

                rows = gp.UpdateCursor(inTable)
                row = rows.next()
                
                try:
                    while row:
                        for i in range(0,numSchools):
                            school[i] = row.GetValue(Elem[i])
                            M[i][1] = school[i]
                
                        M.sort(lambda x, y: cmp(x[1], y[1]))
                        M.reverse()
                
                        if M[0][1] > 0:
                            school = M[0][0]
                            row.SetValue("[ScenarioTemplate_1011." + String + "]", school)
                
                        gp.AddMessage("You made it through the if statement")
                        rows.UpdateRow(row)
                        row = rows.next()
                
                except:
                    gp.AddError("Failed to set the value of new scenario field to new school assignment")


                Thanks.
                • Re: Python script help for row.SetValue() function
                  biniamts
                  hello there ,
                  i guess i have similar problem would appreciate if you have any idea. i want to copy the records of a single row and dublicate it in the same attribute table uisng python script that is first getvalue and then setvalue. i tired first to insert a new row then , put the value of the row to be copied in a list[] and then try to set the values of the new row from the list , but it did not work. any idea ...
                  ...............................................
                  rows = gp.UpdateCursor(fc)
                  cur = gp.searchcursor(fc)
                  row = cur.next()
                  i = 0
                  while row:
                      if row.Urban ==22:   # this is just to check if it is the right row
                          fields = gp.Listfields(fc)
                          field = fields.next()
                          i = i
                          while field:
                              value = Store[i]                          # store is the collection of records from the row to be copied
                              row.setvalue("(field.name)", value)
                              #rows.UpdateRow(row)
                              i+=1
                              field = fields.next()
                          del field
                      rows.UpdateRow(row)
                      row = cur.next()
                  del row
                  del rows 
                  ....................................................
                  thanx
                  • Re: Python script help for row.SetValue() function
                    mdenil
                    to biniamts;
                    You cannot add a row with an Update Cursor. Only InsertCursor has a new row method.
                    try running your search cursor first, and building either a nested list (if the existing records are in the order you would like to write them) OR building a dictionary (if you want to reference the records by a unique key value). Run an InsertCursor from whichever you built to add the new duplicated records.
                    • Re: Python script help for row.SetValue() function
                      gnewburyiv
                      Also be sure to check your casing:

                      the methods for Getting and Setting Values are:

                      getValue(field_name)
                      setValue(field_name, object)

                      --Took this straight for ArcGIS help

                      Some of the posts in this thread had the wrong casing (e.g. 'SetValue')

                      -George
                      • Re: Python script help for row.SetValue() function
                        mdenil
                        Yes, that is a real concern with Arc10. Arc9 was indifferent to capitalization on geoprocessor comands, but Arcpy is a stickler.