1

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.

asked Jun 26, 2018 at 17:39
2
  • 1
    I'd recommend having a single 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 of val 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 (ie types = {'School':['CollegesUniversities',...]}). Then you could loop through the keys and if val in types[key]: row[1] key, etc. Commented Jun 26, 2018 at 18:27
  • 1
    I see nothing incorrect in your code. Try adding a print statement as suggested in previous comment to make sure 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. Commented Jun 26, 2018 at 18:52

1 Answer 1

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.

enter image description here

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])

answered Jun 27, 2018 at 8:21
1
  • 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 workspace Commented Jul 2, 2018 at 22:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.