Removing Leading Numbers From a Field

3758
22
04-28-2022 06:50 AM
jfkuhn
by
New Contributor

I have a tracts field (Tract) which I want to strip off the first 4 numbers in an attribute table.  I created a new field (Tract2) for it.  I've tried several commands without success using the field calculator.  I'm receiving errors 

0 Kudos
22 Replies
jcarlson
MVP Esteemed Contributor

It's not horrible if you come from a JS background, but I would agree that if you've got the option to use Python, it's usually the better choice. "Couldn't I just use Python for this?" is something I utter pretty frequently when I'm working with web layers.

- Josh Carlson
Kendall County GIS
0 Kudos
jfkuhn
by
New Contributor

curtvprice

Example also produced an error

0 Kudos
curtvprice
MVP Esteemed Contributor

Read the fine manual for the Calculate Field tool for more details on how to use the expression and code block.

Also if your tract is indeed a number (this is incorrect, you should try to get them read as text) you can string it to fix the problem, convert to string first, by wrapping the expression like this: f(str(!Tract!))

Make sure your Tract2 is a text field or you will lose leading zeros.

0 Kudos
curtvprice
MVP Esteemed Contributor

If your data are numbers, this Python expression should work without a code block. Assuming Tract2 is a text field, which it should be.

str(!Tract!)[4:]

jfkuhn
by
New Contributor

That work accept it puts a .0 at the end.  I need it without a .0 so I can join it with another table

0 Kudos
jcarlson
MVP Esteemed Contributor
str(!Tract!)[4:-2]

Can I ask where the original source of this data is? Did you by chance import a CSV or text file into Pro or something? It sounds an awful lot like a text-based data source being incorrectly interpreted as a number, which happens when a field only contains digits.

This is beyond the scope of the original question, but you may need to address upstream issues with how your data is imported. As @curtvprice notes, it really is supposed to be a text field.

- Josh Carlson
Kendall County GIS
jfkuhn
by
New Contributor

Still get errors.  See attached

0 Kudos
jcarlson
MVP Esteemed Contributor

Hrm. Can you confirm whether tract is coming in as a string or number? In any case, try explicitly changing it. Replace line 1 of the expression with this:

var t = Text($feature['Tract'])

 

- Josh Carlson
Kendall County GIS
0 Kudos
jfkuhn
by
New Contributor

The following work except it puts a .0 in at the end.  I need it without the .0 so I can join it with another table.

If your data are numbers, this Python expression should work without a code block. Assuming Tract2 is a text field, which it should be.

str(!Tract!)[4:]

0 Kudos
jfkuhn
by
New Contributor

  The original data source is numerical (double) so when I create a text field it puts .0 at the end.  The data is a shape file.  The .0 prevents me from joining it with the Census Tract data I downloaded.  So, how can drop the .0 in the newly created text Tract Field from Python?

0 Kudos