I want to use "Calculate field" based on the string in another field. I would like to loop if condition but something is wrong and I am little bit confused.
I am super new in Python. Here is my simple code:
import arcpy
stands = r'D:\mountaine\database.mdb\testPoly'
arcpy.MakeFeatureLayer_management(stands,'pormap')
with arcpy.da.SearchCursor("pormap", [ 'TEXTSTR', 'NUMBR']) as searchIng:
for x in searchIng:
if x[0] == 'BK':
arcpy.CalculateField_management("pormap", "RESULTS", "[NUMBR]*1")
elif x[0] == 'SM':
arcpy.CalculateField_management("pormap", "RESULTS", "[NUMBR]*2")
else:
arcpy.CalculateField_management("pormap", "RESULTS", "[NUMBR]*3")
All I want is to multiply number from "NUMBR" field based on string in "TEXTSTR" field. Can someone tell me where I'm having a mistake ?
Here is my table:
-
1you need to use UpdateCursor pro.arcgis.com/en/pro-app/arcpy/data-access/…NULL.Dude– NULL.Dude2018年03月07日 20:46:47 +00:00Commented Mar 7, 2018 at 20:46
3 Answers 3
Try using an UpdateCursor:
import arcpy
stands = r'D:\mountaine\database.mdb\testPoly'
arcpy.MakeFeatureLayer_management(stands,'pormap')
with arcpy.da.UpdateCursor("pormap", [ 'TEXTSTR', 'NUMBR', 'RESULTS']) as searchIng:
for x in searchIng:
if x[0] == 'BK':
x[2] = x[1]*1
elif x[0] == 'SM':
x[2] = x[1]*2
else:
x[2] = x[1]*3
searchIng.updateRow(x)
Note: this is untested, so be careful of any typos I made.
I think you have a misunderstanding of cursors and calculate field, these methods are mutually exclusive (if you use one don't use the other).
The answer by Richard Morgan is correct, however there is another way, by inserting a code block into calculate field (read more about code blocks); If you specifically need to use Calculate Field and not an update cursor:
arcpy.CalculateField_management("pormap","RESULTS","calcThis( !TEXTSTR!,!NUMBR!)","PYTHON_9.3","def calcThis( Text,Number):/n if Text == 'BK':/n res = Number/n elif Text == 'SM':/n res = Number * 2/n else:/n res = Number * 3/n return res/n")
This does seem a little long winded but you can insert new lines into the python codeblock and it will (should) still work just fine:
CodeBlock ="""
def calcThis( Text,Number):
if Text == 'BK':
res = Number
elif Text == 'SM':
res = Number * 2
else:
res = Number * 3
return res
"""
arcpy.CalculateField_management("pormap","RESULTS","calcThis( !TEXTSTR!,!NUMBR!)","PYTHON_9.3",CodeBlock)
How it looks in field calculator: enter image description here
If you need to write a lot of if statements another option is to use a Dictionary:
The main operations on a dictionary are storing a value with some key and extracting the value given the key.
This way you only need one if statement which is to check if a key exists in the dictionary:
import arcpy
stands = r'D:\mountaine\database.mdb\testPoly'
arcpy.MakeFeatureLayer_management(stands,'pormap')
d = {'BK': 1, 'SM':2} #Dictionary. Can contain thousands of key:values.
with arcpy.da.UpdateCursor('pormap', ['TEXTSTR', 'NUMBR', 'RESULTS']):
if row[0] in d: #Check if TEXTSTR key is in dictionary
row[2] = row[1] * d[row[0]]
else:
row[2] = row[1] * 3
cursor.updateRow(row)