3

I have zero experience with python and very little experience with ModelBuilder in ArcGIS. I have a shapefile with two fields, let's say their names are "Function" and "Regulation". What I need to do is to select by attributes from the field "Function" and write a specific text in the field "Regulation". Now I'm doing it manually, e.g. I select by attributes where "Function" is 'sport' and then via field calculator I write the text into the field "Regulation". Then I select by attributes other fields in "Regulation" and so on. What I want to do is to automate the whole process, because in the future I'll do this for other shapefiles with similar values in the fields, so my idea was to have a script, where I would only rewrite the values which are different.

Any ideas how to do it?

I've tried these scripts:

Expression:
Reclass (!Regulation!)

Code Block:
def Reclass (Function):
    if (Function == "sport"):
        return "400_TXT\888_B.txt"
    elif (Function == "water"):
        return "400_TXT\888_D.txt"

and this one:

 def Reclass (Function):
    dict = {
    "sport" :"400_TXT\888_B.txt"    ,
    "water" :"400_TXT\888_D.txt"    ,
    } 

I had an error "field is not nullable", so I tried these scripts in the feature class in gdb, but the fields were not updated.

  • your code doesn't look too bad, but you should call Reclass(!Function!) instead of Reclass(!Regulation!) with "claculate field on the "Regulation" field. Then adding an "else" clause (return "") would be nice and I would write "400_TXT\\888_D.txt", but this is not necessary. – radouxju Apr 3 '18 at 13:57
5

You can try the edit to you code below:

def Reclass(Function):

        if (Function == "sport"):
            return "400_TXT\888_B.txt"
        elif (Function == "water"):
            return "400_TXT\888_D.txt"
        else:
             return " "

"field is not nullable": The field requires data in all rows... hence the last statement..."something here". Or you can create a new nullable field. You can call the function as in the image below.

enter image description here

EDIT: Updated based on comments by radouxju to OP.

  • Thank you so much! It is working. Now I have problem only with encoding - in my atrribute table there are characters like š,č etc. and it is not working for these fields. I have tried to write # -- coding: utf-8 -- before the script, but it is not working. Do you have any ideas how to solve this? Thank you in advance. – Lucy Apr 5 '18 at 12:14
  • 1
    You did 90% of the work with the code in your original post. Great that it works for you. You may need to post another separate question to the forum regarding the encoding... just part of the rules of the forum. – ZrSiO4 Apr 5 '18 at 13:09
  • Thank you, I'll try to ask or solve it by myself. Anyway thanks a lot for your help, I'm really grateful :) – Lucy Apr 6 '18 at 13:16
4

If you want to use Python use da.UpdateCursor instead of Field Calculator. When using a dictionary you need to check if the key exists in the dictionary or code will possibly fail:

import arcpy
feature_class = r'C:\database.gdb\features123'
fields = ['Function','Regulation']

d = {"sport" :r"400_TXT\888_B.txt","water" :r"400_TXT\888_D.txt"} 

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if row[0] in d:
            row[1] = d[row[0]]
            cursor.updateRow(row)
        else:
            print '{0} not found in dictionary, no update!'.format(row[0])
            #Or if field is not nullable instead:
            #row[0] = 'Not found'
            #cursor.updateRow(row)
  • Thank you for your help! I didn't try it, because the solution from another user was fine, but thanks anyway! – Lucy Apr 5 '18 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.