I'm working on a script that will use a predefined list of strings to calculate the value of a new field. The idea is this: if the value of field 'EF_Type' is equal to the value of a string in the field 'Schools', then the new field 'EF_Group' will be calculated to equal 'Schools.'
School = ['CollegesUniversities', 'PrivateSchools', 'PublicSchools', 'SupplementalColleges']
EmergencyServices = ['EMS_Stations', 'Hospitals', 'UrgentCare', 'RedCross']
for Sublist in [PointsInt, LinesInt]:
for fc in Sublist:
fcname = os.path.basename(str(fc.getOutput(0)))
with arcpy.da.UpdateCursor(fc, ['EF_Type', 'EF_Group']) as cursor:
for row in cursor:
val = str(row[0])
if val in School:
row[1] = 'School'
cursor.updateRow(row)
elif val in EmergencyServices:
row[1] ='Emergency Services'
cursor.updateRow(row)
When I run my script, no error is thrown, but the second field does not update with the new value. What am I doing wrong? I've based this off answers to similar questions here, but can't seem to troubleshoot the issue.
1 Answer 1
I would take EvilGenius's advice without dealing with cursors, simply using Field Calculator
. However, if this is a part of a larger script where the use of cursor is a must, then the answer would change. See the snip below. Basically there is a python dictionary to keep your entries in EF_Type
and the bottom part tries to find these entries, and if it can, it populates as you wish, otherwise it gets empty.
Here are the codes:
CODE BLOCK:
type_dict = {('CollegesUniversities', 'PrivateSchools', 'PublicSchools', 'SupplementalColleges') : "School", ('EMS_Stations', 'Hospitals', 'UrgentCare', 'RedCross') : "EmergencyServices"}
EXPRESSION:
''.join([v for k,v in type_dict.items() if !EF_Type! in k])
-
Thanks, everyone. I cleaned up the cursor and used the field calculator to loop through the dictionary, but I'm still getting no updates. I'll go through and make sure I'm appending the results to the right list, but it seems like it's not updating at all. Is this possibly a bug in working in the in_memory workspace? Earlier I was able to solve a weeks-long issue with a mass merge by simply copying features to a feature class out of the in_memory workspaceuser121762– user1217622018年07月02日 22:35:53 +00:00Commented Jul 2, 2018 at 22:35
cursor.updateRow(row)
out at the end, outside of your if, elif blocks instead of individual ones, but I don't think that would cause it not to work. Have you tried printing out some of the values ofval
to see if you are getting the expected values? Also, using a dictionary, with keys equal to your type and values with your lists would be easier if you have many types to enumerate (ietypes = {'School':['CollegesUniversities',...]}
). Then you could loop through the keys andif val in types[key]: row[1] key
, etc.str(row[0])
is what you are expecting. Also make sure EF_Group is correct field type and length. You could also try:val = row[0].replace(' ','').upper()
and make all items in list uppercases to make sure no whitespaces or letter case is causing the ifs not to match.