Replace in Field Calculator

1874
10
Jump to solution
03-04-2020 03:56 PM
JustinNettleton1
New Contributor III

Hello, I am trying to move an address number field from a text to an integer field. The issue I am having is some of the addresses in the text field have spaces in them followed by a unit number. I already have another field with unit numbers so trying to figure out a way to mass updates these features. There is about 85000 of them. 

Address Num example: 12345 01

Is there some sort of function (replace, trim?) where I can search for the space and delete everything after it in my text address number field?

Thanks, 

Justin 

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

do you by chance have a newline character in there?

print(a)
1234
56

a.replace("\n","").split(" ")[0]
'1234'

If the values aren't stacked in the first place and replacing a single space with nothing doesn't work, then you may have multiple spaces as a separator or esoteric non-printable characters as a separator

At which point.... slice how many characters off that you want from whatever end

a = "1234 56"

a[:4]      # 1234

a[-2:]     # 56

View solution in original post

0 Kudos
10 Replies
DanPatterson_Retired
MVP Emeritus
"12345 01".split(" ")[0]   # ---- keep as string
'12345'

int("12345 01".split(" ")[0])  # ---- convert to integer
12345

might be what you are looking for, in field calculator-ese or the 2nd one if you want integers

!YourFieldWithTheValues!.split(" ")[0]
JustinNettleton1
New Contributor III

Dan, 

the calculation worked but now it is showing as 12345 *0. 

Doesn't look like it deleted anything after the space. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

split, splits on the space [0] keeps the first bit, so [1] will keep the second bit.

Now if you have nulls and other 'stuff' in there, then either query for the good stuff only, or the field calculation will have to be edited

0 Kudos
JustinNettleton1
New Contributor III

ok, but I don't want to keep anything after the space. I want the space any anything after it deleted from the field. 

I don't have any nulls or anything. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

"12345 01".split(" ")[0]
'12345'

visually, splitting values in a 'junk' field so that values appear in the 'More_Junk' field

0 Kudos
JustinNettleton1
New Contributor III

Following your instructions it still isn't getting rid of anything. As you can see it is stacking the characters after the space. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

So your source field is also called ...Junk... ??

Maybe you had better show what is in that field as well

0 Kudos
JustinNettleton1
New Contributor III

My source field is Call AddrNumber, I just copied it a new field called junk to copy exactly how you had it. 

Source field looks the same as the new junk field after the calculation. 

Sou

0 Kudos
DanPatterson_Retired
MVP Emeritus

do you by chance have a newline character in there?

print(a)
1234
56

a.replace("\n","").split(" ")[0]
'1234'

If the values aren't stacked in the first place and replacing a single space with nothing doesn't work, then you may have multiple spaces as a separator or esoteric non-printable characters as a separator

At which point.... slice how many characters off that you want from whatever end

a = "1234 56"

a[:4]      # 1234

a[-2:]     # 56

0 Kudos