I have a field, "Access" that I want to populate with 4 variables based on the attribute of another field, "Des_Tp".
import arcpy
from arcpy import env
env.overwriteOutput = True
inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
OA_list = ["NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"]
RA_list = ["NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"]
UK_list = ["MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"]
XA_list = ["WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE"]
fields = ['Des_Tp', 'Access']
expression = '"Access_Src"' + "='" + "GAP - Default" + "'"
dm.MakeFeatureLayer(inFeatures, "TEMP_LYR")
dm.SelectLayerByAttribute ("TEMP_LYR", "NEW_SELECTION", expression)
for i in range (0,63):
with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
for row in cursor:
if row [0] == OA_list[i]:
row [1] = "OA"
elif row [0] == RA_list[i]:
row [1] = "RA"
elif row [0] == UK_list[i]:
row [1] = "UK"
elif row [0] == XA_list[i]:
row [1] = "XA"
cursor.updateRow(row)
del cursor
dm.Delete("TEMP_LYR")
ForFor example, if a feature withhas "Des_Tp" = 'NP" will be assigned an access of "OA". When I run the code'NP', I encounter the error
Traceback (most recent call last):
File "C:/Users/A/Desktop/Access_test.py", line 34, in <module>
elif row [0] == UK_list[i]:
IndexError: list index out of range
then I currently designate (0,63)would want to code it as the range because that is how many variables I haveopen access, 'OA' in the "Des_Tp" field, but I may be interpreting this line incorrectly "Access".
As Paul commented below, it might be possible to simplify the code with a dictionary. I've put them listthe lists in a dictionary, but the code runs without any change in the data:
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
inFeatures = r"C:\Users\A\AccessTest.gdb\Access_test"
destp_dict ={1:("NP","NM","NCA","NF","NG","PUB","NT","WSR","NRA","NLS","IRA","ACEC","REC","RMA","ACC","OCS","SP","SREC","SLHCA","SRMA","LP","LREC","LHCA","LRMA"),
2:("NWR","WA","WSA","NSBV","RNA","REA","HCA","SDA","SW","SCA","LCA"),
3:("MPA","PROC","FOTH","ND","TRIBL","SO","LO","UNK"),
4:("WPA","MIT","MIL","PCON","PREC","PHCA","PAGR","PRAN","PFOR","PO","PO","CONE","RECE","HCAE","HCAE","AGRE","RANE","FORE","OTHE","UNKE")}
fields = ['Des_Tp', 'Access']
access_type = ["OA", "RA", "UK", "XA"]
dmarcpy.MakeFeatureLayer(inFeatures, "TEMP_LYR")
with arcpy.da.UpdateCursor ("TEMP_LYR", fields) as cursor:
for row in cursor:
if row[0] == destp_dict[1]:
row[1] = "OA"
if row[0] == destp_dict[2]:
row[1] = "RA"
if row[0] == destp_dict[3]:
row[1] = "UK"
if row[0] == destp_dict[4]:
row[1] = "XA"
cursor.updateRow(row)