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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Oct 27, 2017 at 20:37

3 Answers 3

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.

answered Oct 27, 2017 at 21:05
3
  • 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". Commented Oct 27, 2017 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. Commented Oct 27, 2017 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. Commented Oct 27, 2017 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.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Oct 27, 2017 at 22:00
0
def LyrLineTypeCatEye(field):
 if "01" in filed:
 return field.replace("01","unic")
 else:
 return field‍‍‍‍‍‍‍‍‍‍
LyrLineTypeCatEye(!Pattern_Type!)

I did exactly like you said and I don't understand why it's not working!

menes
1,4272 gold badges8 silver badges25 bronze badges
answered May 27, 2021 at 7:30
1
  • Welcome to Geographic Information Systems! If you have a NEW question, please ask it by clicking the Ask Question button. If you have sufficient reputation, you may upvote the question. Alternatively, "star" it as a favorite and you will be notified of any new answers. Commented May 27, 2021 at 8:37

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.