import arcpy, datetime
arcpy.env.overwriteOutput = True
# Number of days to subtract
tdelta = datetime.timedelta(days=21)
fiberCable = r'C:\Users\broadhurstn\Documents\ArcGIS\Default.gdb\FIBERCABLE'
conduit = r'C:\Users\broadhurstn\Documents\ArcGIS\Default.gdb\CONDUIT'
fibercableToConduit = r'C:\Users\broadhurstn\Documents\ArcGIS\Default.gdb\FibercableToConduit'
unc = r'\\Bvweb104draco\e\08_Scripts\referenceScripts\Conduit Export\ConduitExports.gdb\TampaConduit'
# Populate a dictionary that maps segment ID to the placed/planned date
segment_dates = defaultdict(list)
with arcpy.da.SearchCursor(fiberCable, ['segmentid', 'bv_cable_placed_planned']) as cursor:
for row in cursor:
segment_dates[row[0]].append(row[1])
# Populate a dictionary that maps conduit ID to a two-item list containing segment ID and placed/planned date
ftcs = {}
with arcpy.da.SearchCursor(fibercableToConduit, ['conduitipid', 'segmentid']) as cursor:
for row in cursor:
# Use .get() so that it won't fail if a segment ID isn't in the dictionary
ftcs[row[0]] = [row[1], segment_dates.get(row[1])]
# Populate conduit 'bv_placed_est' field and subtract 21 days
with arcpy.da.UpdateCursor(unc, ['ipid', 'bv_placed_est']) as cursor:
for row in cursor:
ipid = row[0]
for k,v in ftcs.items():
if ipid == str(k):
# tdelta will not work on null values
if v[1] is not None:
row[1] = min(v[1]) - tdelta
cursor.updateRow(row)
The above code and below feature class table produce a dictionary that fills as follows:
{u'248bdd3a-a13c-44b4-b592-b64a843fb0bc': [u'{2AC71409-C59E-49E1-A35B-61CD19885A2A}', datetime.datetime(2020, 1, 14, 0, 0)]}
How do I add a multiple values to the dictionary keys using the below feature class table and have it choose the earliest date of the bunch?
Notice how the value in the dictionary matches "segment ID" in this feature class. I want to have the the SearchCursor look at the current dictionary, see if there is a value that matches the "SegmentID" field values and append the EARLIEST "Cable Placed Planned" to it's corresponding dictionary item as a second value. There could be multiple 'Cable Placed Planned' dates in the form of a value associated with a key.
Expected output for the first item:
{u'248bdd3a-a13c-44b4-b592-b64a843fb0bc': [u'{2AC71409-C59E-49E1-A35B-61CD19885A2A}', datetime.datetime(2016, 1, 14, 0, 0), datetime.datetime(2017, 12, 14, 0, 0), datetime.datetime(2019, 10, 28, 0, 0)}, etc...
The key could have 1 value, or 20 values associated with it and I need to sift through those values for the earliest date and append it to the conduit feature class field.
Working code above only takes a single value per key, but can't deal with instances when there are numerous values that need to be associated with a key.
in
to see if the key is already there, and then appending to the list, otherwise initialize it with a value that is a list with just one element. mkyong.com/python/python-check-if-key-exists-in-dictionarydatetime.datetime(2020, 1, 14, 0, 0)
and then want to add values from another feature class? What happened to the datetime value first added, cant see it in your example