>>> phoneBook = {'Bob': 4566461, 'Jane': 8954556}
>>> phoneBook['Bob'] 4566461
# userValue represents what the user selected, i.e. userValue = 'Alpine' officeCodeDict = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA'} # databaseValue is what you send to the database databaseValue = officeCodeDict[userValue] # so, if userValue is Alpine, databaseValue is AL
with arcpy.da.UpdateCursor("Stewardship", ("Office", "Forester")) as rows
################################################## # # #Script: Attribute Table for reporting (Stewardship) # ################################################## #Import modules import os, sys, arcpy, traceback, arcgisscripting #Set Map Document mxd = arcpy.mapping.MapDocument("Current") #Set Overwrite Option arcpy.env.overwriteOutput = True df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] gp = arcgisscripting.create(10.1) #del row #del rows try: #Sets parameters (attributes) Office = gp.GetParameterAsText(0) Forester = gp.GetParameterAsText(1) officeDomain = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA', 'Corpus Christi': 'CC', 'Conroe': 'CO', 'Crockett': 'CR', 'Crockett':'CR', 'College Station': 'CS', 'Canyon': 'CY', 'Dallas': 'DA', 'El Paso': 'EP', 'Fort Worth': 'FW', 'Gilmer': 'GI', 'Granbury': 'GR', 'Hamilton': 'HA', 'Henderson': 'HE'} officeCode= officeDomain[Office] foresterDomain = {'Brittany Compton': 'bcompton', 'Brian Pope': 'bpope', 'Buster Robinson': 'brobinson', 'Clay Bales': 'cbales', 'Daniel Duncum': 'dduncum', 'Daniel Lewis': 'dlewis'} foresterCode = foresterDomain[Forester] #Create Update Cursor with arcpy.da.UpdateCursor("Stewardship", (Office, Forester)) as rows: # row comes back as a tuple in the order specified here, so Office is row[0], Forester is row[1] for row in rows: row[0] = officeCode row[1] = foresterCode rows.updateRow(row) #Refresh Map to reflect Changes arcpy.RefreshActiveView() mxd.save() del mxd, except: tb = sys.exc_info()[2] tbinfo = traceback.format_tb(tb)[0] pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n " + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n" msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n" arcpy.AddError(msgs) arcpy.AddError(pymsg) print msgs print pymsg arcpy.AddMessage(arcpy.GetMessages(1)) print arcpy.GetMessages(1)
Start Time: Mon Jul 08 09:07:36 2013 Running script Stewardship32... ARCPY ERRORS: PYTHON ERRORS: Traceback Info: File "D:\ArcGISData\SARS\Python_10April2013\BoundaryReporting_26March2013_changes_july2013_added_domain_changed_again.py", line 49, in <module> for row in rows: Error Info: <type 'exceptions.RuntimeError'>: Cannot find field 'Austin'
#Create Update Cursor with arcpy.da.UpdateCursor("Stewardship", (Office, Forester)) as rows: # row comes back as a tuple in the order specified here, so Office is row[0], Foester is row[1] for row in rows: row[0] = officeCode row[1] = foresterCode rows.updateRow(row)
#Create Update Cursor rows = arcpy.UpdateCursor("Stewardship") for row in rows: row.Office = officeCode row.Forester = foresterCode rows.updateRow(row)
# set up the dictionaries officeDomain = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA', 'Corpus Christi': 'CC', 'Conroe': 'CO', 'Crockett': 'CR', 'Crockett':'CR', 'College Station': 'CS', 'Canyon': 'CY', 'Dallas': 'DA', 'El Paso': 'EP', 'Fort Worth': 'FW', 'Gilmer': 'GI', 'Granbury': 'GR', 'Hamilton': 'HA', 'Henderson': 'HE'} foresterDomain = {'Brittany Compton': 'bcompton', 'Brian Pope': 'bpope', 'Buster Robinson': 'brobinson', 'Clay Bales': 'cbales', 'Daniel Duncum': 'dduncum', 'Daniel Lewis': 'dlewis'} #Sets parameters (attributes) Office = gp.GetParameterAsText(0) Forester = gp.GetParameterAsText(1) Status = gp.GetParameterAsText(2) # check parameters are valid if officeDomain.has_key(Office): officeCode= officeDomain[Office] else: arcpy.AddMessage('ERROR: Invalid value entered for Office: %s' % Office) exit() if foresterDomain.has_key(Forester): foresterCode = foresterDomain[Forester] else: arcpy.AddMessage('ERROR: Invalid value entered for Forester: %s' % Forester) exit() ## Cursors, etc.
#Sets parameters (attributes) Office = gp.GetParameterAsText(0) Forester = gp.GetParameterAsText(1) Status = gp.GetParameterAsText(2) officeDomain = {'Alpine': 'AL', 'Austin': 'AU', 'Carthage': 'CA', 'Corpus Christi': 'CC', 'Conroe': 'CO', 'Crockett': 'CR', 'Crockett':'CR', 'College Station': 'CS', 'Canyon': 'CY', 'Dallas': 'DA', 'El Paso': 'EP', 'Fort Worth': 'FW', 'Gilmer': 'GI', 'Granbury': 'GR', 'Hamilton': 'HA', 'Henderson': 'HE'} officeCode= officeDomain[Office] foresterDomain = {'Brittany Compton': 'bcompton', 'Brian Pope': 'bpope', 'Buster Robinson': 'brobinson', 'Clay Bales': 'cbales', 'Daniel Duncum': 'dduncum', 'Daniel Lewis': 'dlewis'} foresterCode = foresterDomain[Forester] #Create Update Cursor rows = arcpy.UpdateCursor("Stewardship") #Update Forester for row in rows: if (officeDomain.has_key(Office)): row.Office = officeCode rows.updateRow(row)
if (officeCodeDict.has_key(userValue)): myVal= siteDict[userValue] else: print "not an acceptable value"
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.