AnsweredAssumed Answered

If/Else Statement Field Calculator

Question asked by geoffreywestgis on Feb 3, 2015
Latest reply on Feb 3, 2015 by xander_bakker

I would like to add a simple if statement to an existing expression, but am having difficulties getting it to execute.  My previous expression in vb script concatenates a number of fields to make one long string in a new field.  I would like to add multiple if else statements to be a part of this expression.

 

'Trim([NUMBERCYLA]) & ",SB,"& Trim([RESOLUTION_CODE]) & ",,,SC Truck,"& Trim([last_edited_user])& ",Driver,"& Month( [last_edited_date])&"/"& Day([last_edited_date])&"/"& Year ( [last_edited_date]) & ","'

 

I have a field SCCatDesc that contains the attributes MAT, MBE, MBI, MBW, SBE, and SBI.

 

If SCCatDesc = MAT then output = SB

If SCCatDesc =  MBE then output = ME

If SCCatDesc =  MBI then output = MB

If SCCatDesc =  MBW then output = MW

If SCCatDesc =  SBE then output = SB

If SCCatDesc =  SBI then output = SBI

 

 

 

I have tried the following as start however it does not work.

 

  1. If [SCCatDesc] = "MAT" Then 
  2.   Output = "SB" 
  3. ElseIf [SCCatDesc] = "MBE" Then 
  4.   Output = "ME" 
  5. Else 
  6.   Output = "TEST"
  7. End If 

 

 

 

import arcpy

#Define Local Parameters
whereclause = "PlannedDate = CONVERT(DATE, GETDATE())"
SDEFeatureClass = "C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Connection to localhost_SCData_sa.sde\SCData.DBO.SO_SC"
LocalFGDB = "C:\PeterText.gdb"
Outable = "WLATable"
PeterTable = "C:\PeterText.gdb\WLATABLE"
expression = 'Trim([NUMBERCYLA]) & ",SB,"& Trim([RESOLUTION_CODE]) & ",,,SC Truck,"& Trim([last_edited_user])& ",Driver,"& Month( [last_edited_date])&"/"& Day([last_edited_date])&"/"& Year ( [last_edited_date]) & ","'

#Selection Query
selection = """CYLA_DISTRICT = 'WLA' AND PlannedDate = CONVERT(DATE, GETDATE())"""







#Deletes old FC
if arcpy.Exists(PeterTable):
  arcpy.Delete_management(PeterTable)


#Sends Feature Class to Table with Where Clause
arcpy.TableToTable_conversion(SDEFeatureClass, LocalFGDB, Outable, selection)

#Calculates Field with expression for Peter Text File
arcpy.CalculateField_management(PeterTable, "PtrText", expression)










#Search Cursor to extract Peter Text Field
myOutputFile = open("C:\PeterScripts\WLA\Peter.txt", 'w')
rows = arcpy.da.SearchCursor("C:\PeterText.gdb\WLATable", ["PtrText"])
for row in rows:
  myOutputFile.write(str(row[0]) + '\n')
del row, rows
myOutputFile.close()

 

 

 

 

 

 

 

 

Outcomes