I have been looking around the internet all morning to figure out how to write a Python script to accomplish what I need to do, and so far, I am not finding what I need. Need to see if someone can start me in the correct direction.
I have (2) text fields:
ACALARA TYPE and ACALARA MODEL with coded meter type attributes
Another field:
ACALARA METER TYPE will hold the actual Meter Type
The data in the first two fields is held in a different database and I have a Python script built that will take that data and update these Feature Class Fields.
What I need to do is build a second python script that is composed of an if/else statement.
EXAMPLE:
If ACALARA TYPE = 75 and ACALARA MODEL = 130
Then the ACALARA METER TYPE field will update to show "CEN3S4S20"
If ACALARA TYPE = 109 and ACALARA MODEL = 2
Then the ACALARA METER TYPE field will update to show "UMT2.1"
There are around 2 dozen other combinations with additional Meter Type outputs that I will have to include
Figuring out how to write this code will help with other Feature Classes that I have been updating manually.
Thank You
You can do this with the Calculate Field tool with a code block. I highly recommend using Python for many reasons.
Expression
proc(!ACALARA_TYPE!, !ACALARA_MODEL!)
Code block
def proc(actype, acmodel):
# try except block handles non-handled cases!
try:
if actype == 75 and acmodel == 130:
result = "CEN3S4S20"
elif actype == 109 and acmodel == 2:
result = "UMT2.1"
else:
# we ran into an uncovered case
raise(Exception)
except:
result = "undefined"
return result
Curtis:
Thank you for the reply and the script. I tried entering it into Calculate field through the FC Table. However, all of the fields in the ACALARA METER TYPE field got filled in with "undefined" from the last part of the script.
There is probably an issue with your code (or mine); any error in your code will give you "undefined" because I'm trapping all exceptions.
Until you get it working on one, set up except block to do this, then you'll get an error message.
except:
raise ## remove this line after you get it working
result = "undefined"
return result
I'm not 100% clear on the rules, but some of the lines in the solution above have a 3-space indent, while others have 4. I believe that once you establish a 4-space indent, you need to continue with that.
You sure do. I think I'm having trouble with the new highlight tool. I will do it an IDE window and paste in.
I know there is a way to run this process through Arcpy Update Cursor. However, I am not sure exactly how to start the code.
If you are really using spaces in your field names, I highly recommend not doing so. https://community.esri.com/people/curtvprice/blog/2016/05/07/how-to-name-things-in-arcgis?sr=search&...
In the python window, or in a script you can do this, it will run on the selected features in your layer or table view (as does Calculate Field):
UPDATE - fixed my exception handling - it was broken.
def proc(actype, acmodel):
# try except block handles non-handled cases!
try:
if actype == 75 and acmodel == 130:
result = "CEN3S4S20"
elif actype == 109 and acmodel == 2:
result = "UMT2.1"
else:
# we ran into an uncovered case
raise Exception("Unhandled case: proc({}, {})".format(actype, acmodel))
except Exception as msg:
raise Exception(msg) ## remove this line after you get it working
result = "undefined"
return result
flds = ["ACALARA_TYPE", "ACALARA_MODEL", "ACALARA_METER_TYPE"]
with arcpy.da.updateCursor("mylayer", flds) as rows:
for row in rows:
row[2] = proc(row[0], row[1])
rows.updateRow(row)
Today is not my day apparently. I am running this through IDLE/Command Prompt I keep getting a runtime error. This is the code I have for the UpdateCursor. Maybe something is wrong??
fc = "C:\MEWCo GIS System\Electric System\MEWCo_Electric_Model-LOCAL.gdb\Secondary\SERVICE_LOCATION"
fields = ["ACALARA_TYPE", "ACALARA_MODEL", "ACALARA_METER_TYPE"]
def proc(actype, acmodel):
# try except block handles non-handled cases!
try:
if actype == 75 and acmodel == 130:
result = "CEN3S4S20"
elif actype == 109 and acmodel == 2:
result = "UMT2.1"
else:
# we ran into an uncovered case
raise(Exception)
except:
raise ## remove this line after you get it working
result = "undefined"
return result
with arcpy.da.UpdateCursor(fc, fields) as rows:
for row in rows:
row[2] = proc(row[0], row[1])
rows.updateRow(row)
Thank You
What is the exact runtime error?
Also, may not be the issue, but beware the way you're writing paths. You should use raw notation or other valid path syntax to avoid escape characters.
For example:
fc = r"C:\MEWCo GIS System\Electric System\MEWCo_Electric_Model-LOCAL.gdb\Secondary\SERVICE_LOCATION"