Spaces

415
2
Jump to solution
09-21-2022 01:24 PM
SLouq
by MVP Regular Contributor
MVP Regular Contributor

Supposed I have a string '111 SPEASST' in my table but want to make it

111 S PEAS ST 

What is the Python Expression I could use in the Calculate Field parser to accomplish this

Thanks

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DavidSolari
Occasional Contributor III

I assume you're trying to clean up address data, which doesn't have a generic solution. If it's the same error over and over you can use string methods such as .index, .split, .join, slicing etc. to fix the error, here's an example that'll work for your example string:

 

def fixAddr(x):
  sNum, sData = x.split()
  sDir = sData[0]
  sName = sData[1:-2]
  sType = sData[-2:]
  return f"{sNum} {sDir} {sName} {sType}"

 

If you want a generic method, the only technique I can think of is to run your address string through a geocoder and then pull out the component parts from the returned address. This is much more complex than just basic field calculation but it's the most bulletproof method I've found. Good luck!

View solution in original post

0 Kudos
2 Replies
DavidSolari
Occasional Contributor III

I assume you're trying to clean up address data, which doesn't have a generic solution. If it's the same error over and over you can use string methods such as .index, .split, .join, slicing etc. to fix the error, here's an example that'll work for your example string:

 

def fixAddr(x):
  sNum, sData = x.split()
  sDir = sData[0]
  sName = sData[1:-2]
  sType = sData[-2:]
  return f"{sNum} {sDir} {sName} {sType}"

 

If you want a generic method, the only technique I can think of is to run your address string through a geocoder and then pull out the component parts from the returned address. This is much more complex than just basic field calculation but it's the most bulletproof method I've found. Good luck!

0 Kudos
DeanAnderson2
Occasional Contributor II

Here are a bunch of examples of how to play with python strings (slice, modify, etc). 

https://www.w3schools.com/python/python_strings.asp