+ Reply to Thread
Results 1 to 9 of 9

Thread: Python script help for row.SetValue() function

  1. #1
    Jeremy Scott
    Join Date
    Apr 2010
    Posts
    67
    Points
    0
    Answers Provided
    0


    0

    Default Python script help for row.SetValue() function

    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:
    row.SetValue(SchoolLevel, M[0][1])
    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:
    row.SetValue(Scenario_Template_1011.SchoolLevel, M[0][1])
    This didn't work, so then I tried to put the feature class name in quotes:
    row.SetValue("Scenario_Template_1011."SchoolLevel, M[0][1]
    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!

  2. #2
    Russell Kallstrom
    Join Date
    Aug 2010
    Posts
    17
    Points
    0
    Answers Provided
    0


    0

    Default Re: Python script help for row.SetValue() function

    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.
    Last edited by rkallstrom; 09-16-2010 at 12:13 PM. Reason: Misunderstood original post.

  3. #3
    Jeff Lee
    Join Date
    Apr 2010
    Posts
    52
    Points
    2
    Answers Provided
    0


    0

    Default Re: Python script help for row.SetValue() function

    What are M[0][1]? are these values from a python list? if so try,


    row.SetValue(SchoolLevel, M[0] + M[1])
    Jeff Lee
    GIS / Data Specialist
    Halton-Hamilton Source Water Protection
    Campbellville, ON
    Canada

    ArcGIS 9.3
    Windows XP Pro

  4. #4
    Jeremy Scott
    Join Date
    Apr 2010
    Posts
    67
    Points
    0
    Answers Provided
    0


    0

    Default Re: Python script help for row.SetValue() function

    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.

  5. #5
    Jeremy Scott
    Join Date
    Apr 2010
    Posts
    67
    Points
    0
    Answers Provided
    0


    0

    Default Re: Python script help for row.SetValue() function

    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.

    Code:
    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.

  6. #6
    Biniam Weldemichael
    Join Date
    May 2011
    Posts
    8
    Points
    0
    Answers Provided
    0


    0

    Default Re: Python script help for row.SetValue() function

    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
    Biniam W.Michael

  7. #7
    mark denil
    Join Date
    Apr 2010
    Posts
    515
    Points
    284
    Answers Provided
    36


    0

    Default Re: Python script help for row.SetValue() function

    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.

  8. #8
    George Newbury
    Join Date
    Apr 2010
    Posts
    52
    Points
    13
    Answers Provided
    1


    0

    Default Re: Python script help for row.SetValue() function

    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

  9. #9
    mark denil
    Join Date
    Apr 2010
    Posts
    515
    Points
    284
    Answers Provided
    36


    0

    Default Re: Python script help for row.SetValue() function

    Yes, that is a real concern with Arc10. Arc9 was indifferent to capitalization on geoprocessor comands, but Arcpy is a stickler.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts