How to Calculate or use Update Cursor to concatenate fields only for values that are None

766
1
11-28-2016 10:18 AM
GeoffreyWest
Occasional Contributor III

I have two values, one date and one time, I would like to only calculate my new field if the value for the field is NULL.  How do I do so using ArcPy?  My current code reads as.

calcExpression = "!ORIGDTDATE!+\" \"+ !ORIGDTTIME!"
 arcpy.CalculateField_management(self.line_ticket_feature_class,"emergency_overdue",calcExpression,"PYTHON_9.3")
 arcpy.CalculateField_management(self.poly_ticket_feature_class,"emergency_overdue",calcExpression,"PYTHON_9.3")
Tags (1)
0 Kudos
1 Reply
MitchHolley1
MVP Regular Contributor

Something like this should work.

import arcpy

date = 'date_field'
time = 'time_field'
new = 'new_field'
feature = r'path to dataset'

with arcpy.da.UpdateCursor(feature, [new, date, time]) as cursor:
    for row in cursor:
        if row[0] is None:
            row[0] = str(row[1]) + '/' + str(row[2]) # can separate the fields with anything you'd like
            cursor.updateRow(row)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos