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:
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 Answers 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– Bec2017年10月27日 21:18:58 +00:00Commented 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.Dan C– Dan C2017年10月27日 21:27:19 +00:00Commented 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.Bec– Bec2017年10月27日 21:36:23 +00:00Commented Oct 27, 2017 at 21:36
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.
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!
-
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.Ian Turton– Ian Turton2021年05月27日 08:37:53 +00:00Commented May 27, 2021 at 8:37
Explore related questions
See similar questions with these tags.