Python script Update Cursor

6414
4
Jump to solution
11-20-2015 12:29 PM
jameljoseph
New Contributor II

I have a table in my geodatabase that contains fields with some values and empty fields. 

What I am trying to do is first let the user select the table to use, then add values to 2 fields based on values in another field but I am a beginner with python so I was wondering if someone here knows what is wrong with my script.

Thank you

import arcpy

# Get table parameter

param1 = arcpy.Parameter(

    displayname="Roads")

   

# Open table to edit

GeoDBLocation = open (param1, 'r+')

Fields["LABEL", "SYMBOL", "SYM_TYPE"]

# Add data

cur = arcpy.UpdateCursor(GeoDBLocation)

For row in cur:

    If row.getvalue("LABEL") == "H":

        row.setvalue("SYMBOL", "3.01")

        row.setvalue("SYM_TYPE", "S")

    Elif row.getvalue("LABEL") == "L":

        row.setvalue("SYMBOL", "3.02")

        row.setvalue("SYM_TYPE", "F")

    Else:

        row.setvalue("SYMBOL", "0.01")

        row.setvalue("SYM_TYPE", "O")

    cursor.updateRow(row)

       

#Delete cursor

del row

del cur

   

#close file

GeoDBLocation.close

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Jamel,

Try using the arcpy.da.UpdateCursor.  It is much faster that the arcpy.UpdateCursor.  Below is an example:

import arcpy

param1 = arcpy.GetParameterAsText(0)

with arcpy.da.UpdateCursor(param1, ["LABEL", "SYMBOL", "SYM_TYPE"]) as cursor:
    for row in cursor:
        if row[0] == "H":
            row[1] = "3.01"
            row[2] = "S"
        elif row[0] == "L":
            row[1] = "3.02"
            row[2] = "F"
        else:
            row[1] = "0.01"
            row[2] = "0"
        cursor.updateRow(row)

del cursor

You can then add this script to a toolbox.  You can setup your parameters like the screen shot below to allow users to select a feature class to run the script on:

Screen1.PNG

View solution in original post

4 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Jamel,

Try using the arcpy.da.UpdateCursor.  It is much faster that the arcpy.UpdateCursor.  Below is an example:

import arcpy

param1 = arcpy.GetParameterAsText(0)

with arcpy.da.UpdateCursor(param1, ["LABEL", "SYMBOL", "SYM_TYPE"]) as cursor:
    for row in cursor:
        if row[0] == "H":
            row[1] = "3.01"
            row[2] = "S"
        elif row[0] == "L":
            row[1] = "3.02"
            row[2] = "F"
        else:
            row[1] = "0.01"
            row[2] = "0"
        cursor.updateRow(row)

del cursor

You can then add this script to a toolbox.  You can setup your parameters like the screen shot below to allow users to select a feature class to run the script on:

Screen1.PNG

jameljoseph
New Contributor II

thanks jake, this works as well.  Learnign so much in one day here!

0 Kudos
FreddieGibson
Occasional Contributor III

There are quite a few errors in your code. I've outline them below and also included a copy of what the correct code should look like. I'm assuming that you're writing this for a script tool.

import arcpy


''' 
You'd use Parameter if you needed to create one. In this case you need to pull
a value from the tool.
'''
# Get table parameter
param1 = arcpy.Parameter(displayname="Roads") 


'''
open is used to open flat files. This is not going to work with opening a 
file geodatabase. The software will handle opening this for you
'''
# Open table to edit
GeoDBLocation = open (param1, 'r+')


'''
You're using a variable below called fields and it hasn't been defined. Python
also doesn't support indexing with words.
'''
Fields["LABEL", "SYMBOL", "SYM_TYPE"]


# Add data
cur = arcpy.UpdateCursor(GeoDBLocation)


'''
python is a case sensitive languague. The words for, if, elif, and else should 
be lowercase. Also, the getValue method needs a capital V.
'''
For row in cur:
    If row.getvalue("LABEL") == "H":
        row.setvalue("SYMBOL", "3.01")
        row.setvalue("SYM_TYPE", "S")
    Elif row.getvalue("LABEL") == "L":
        row.setvalue("SYMBOL", "3.02")
        row.setvalue("SYM_TYPE", "F")
    Else:
        row.setvalue("SYMBOL", "0.01")
        row.setvalue("SYM_TYPE", "O")
    cursor.updateRow(row)




#Delete cursor
del row
del cur


import arcpy


# Get table parameter
param1 = arcpy.GetParameterAsText(0)
fields = ["LABEL", "SYMBOL", "SYM_TYPE"]


cur = arcpy.UpdateCursor(param1)


for row in cur:
    value = row.getValue("LABEL")
    if value == "H":
        row.setValue("SYMBOL", 3.01)
        row.setValue("SYM_TYPE", "S")
    elif value == "L":
        row.setValue("SYMBOL", 3.02)
        row.setValue("SYM_TYPE", "F")        
    else:
        row.setValue("SYMBOL", 0.01)
        row.setValue("SYM_TYPE", "O")        
        
del row
del cur
jameljoseph
New Contributor II

thanks man, it works perfectly.

learning python on the fly but this certainly helps!

0 Kudos