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
Solved! Go to Solution.
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!
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!
Here are a bunch of examples of how to play with python strings (slice, modify, etc).
https://www.w3schools.com/python/python_strings.asp