I am attempted to calculate a field in a feature class ("Feat_Class_1", by using the count of selected features within another feature class ("Feat_Class_2") as a numerator, while using the value of a field in Feat_Class_1 as the denominator. I am iteratively selecting each row in Feat_Class_1 to calculate this. The script executes successfully right up until the end, when the selected row retains 0 as its value (I am expecting a proportional value). My script is as follows:
## Select Routes which are less than 800m, count the values and retain as denominator feat_class_2_subset = str(feat_class_2+"_short") print feat_class_2_subset arcpy.MakeFeatureLayer_management(feat_class_2, feat_class_2_subset) arcpy.SelectLayerByAttribute_management(feat_class_2_subset, "NEW_SELECTION", "Total_Kilometers < 0.8") result = arcpy.GetCount_management(feat_class_2_subset) count = str(result.getOutput(0)) print count ##<-to confirm that the subset has been successfully selected feat_class_1_subset = str(feat_class_1+"_individual_record") print feat_class_1_subset arcpy.MakeFeatureLayer_management(feat_class_1, feat_class_1_subset) selection_sub_expression = str("'"+SA2_String+"'") print selection_sub_expression selection_expression = str("SA2_String = "+selection_sub_expression) print selection_expression arcpy.SelectLayerByAttribute_management(feat_class_1_subset, "NEW_SELECTION", selection_expression2) test_results = arcpy.GetCount_management(feat_class_1_subset) test_count = str(test_results.getOutput(0)) print test_count ##<-to determine if the subset has been selected properly exp = str(count+"/!Total_Properties!") print exp arcpy.CalculateField_management(feat_class_1_subset, "Train_800m_Perc", exp, "PYTHON", "")
This works right up until the "test_count" component but then fails to actually execute the calculation.
Any ideas on where I'm going wrong? I've been careful to ensure my expression is a string for inclusion in the function.