4

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:

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 7, 2018 at 20:04
1

3 Answers 3

8

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.

answered Mar 7, 2018 at 20:19
5

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

answered Mar 7, 2018 at 22:09
1

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)
answered Mar 8, 2018 at 6:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.