1

I'm new to python and trying to write a python script using the field calculator in ArcMap for a specific field in an attribute table that will replace some of the values in the field but leave the other values as they are within the table.

For example: ""fourth" must be changed to "4th" whereas "Neilston" should remain as it is in the field. The field in the table looks like this:

enter image description here

The python script used in the codeblock looks like this:

def streetNUM(field):

   if field == "fourth":

      field.replace ("fourth","4th")

   elif field == "fifth":

      field.replace ("fifth","5th")
   else:
      return field

And then in the box below the codeblock I have:

streetNUM(!FULLNAME!)

Nothing seems to change and I get an error code saying:

Field is not nullable [FULLNAME]

Any suggestions as to where I may be going wrong with my code?

3

You have a couple things going on in your IF and ELIF conditions:

 if field == "fourth":

This line will only be true if the value in your field is "fourth", in lowercase. "Fourth" wouldn't satisfy this condition, neither would "Fourth St." or "fourth street".

 field.replace ("fourth","4th")

You need to prefix this with return. Right now this line will replace "fourth" (again, only if it's lowercase) with "4th", but you need your function to return that value.

  • I made the changes you suggested and when I run it again in field calculator, I don't get any error messages, but none of the values change in the table. The word "Fourth" still shows as "Fourth" and not "4th". – Bec Oct 27 '17 at 21:18
  • @Bec If you're trying to do this on an existing field, you'll need to use an Update Cursor to do it. I'm pretty sure Field Calculator can't be used to update values in an existing field at the same time that field is being used as the source for the calculation. You could create a new text field in your attribute table (FULLNAMEv2 or something) and do the field calculation there. – Dan C Oct 27 '17 at 21:27
  • I know it can be done outside of using the codeblock in field calculator as I have seen that from a classmate who typed in !FULLNAME!.replace("Fifth","5th").replace("Sixth","6th")...going through all the variables that needed changed and got the expected results. Our instructor is expecting this to be done via a python script in the codeblock though for the "FULLNAME" field. There is no mention of creating a new text field. – Bec Oct 27 '17 at 21:36
1

With help from another forum, by changing:

if field == "Fourth":

to:

if "Fourth" in field: 

and then the previous suggestion of adding the prefix return in front of

field.replace("Fourth","4th")

the field calculator tool ran as expected.

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.