do you mean like int(str) or float(str)... I never liked Val("1") when you wanted a float or Val("1.0") when you wanted an integer. The explicit casting of int(str) and float(str) are nice and for the bold.... complex("1") => (1+0j) even does casting into the complex plane
This is of no help, but I'd like to mention that MapBasic/MapInfo has had the function Val(str), from the start. And it has been of great use to me. Today I miss it in ArcGIS. Incidently, not even FME or Excel seem to have a straightforward transformer or module to do this. Strange indeed.
from Phil's 2nd post... where a solution works 95% of the time.. perhaps find a solution for it and then determine whether the remaining 5% has unique characteristics that the first 95% doesn't share... Often a two stage solution is faster and more elegant than a one-pass run. It would be interesting to see this type of dataset broken down on those lines. I am sure that people dealing with geocoding all the time, can come up with examples where failure of the one-step solution occurs all the time.
def get_num_from_string(string): '''This function retrieves numbers from a string and converts them to integers''' # Create empty string to store numbers as a string num = '' # Loop through characters in the string for i in string: # If one of the characters is a number, add it to the empty string if i in '1234567890': num+=i # Convert the string of numbers to an integer integer = int(num) return integer
>>> text = 'Value = -12.214' >>> num = get_num_from_string(string) >>> num 12214
get_num_from_string(!ADDRESS!)
Thank you, Xander. I am totally new to Python and am finding that it is not as intuitive as I had hoped. But, I will see if I can apply what you sent to ArcMap. Maybe I will get ambitious and take one of ESRI's online courses on Python. Best wishes.Phil
import re text = 'There is some 234198372 data in here' print re.findall('\\d+', text)
['234198372']
>>> str = "h3110 23 cat 444.4 rabbit 11 2 dog" >>> [int(s) for s in str.split() if s.isdigit()] [23, 11, 2]
This will not recognise floats, negative integers, or integers in hexadecimal format.
lstRoads = ['US Hwy 1', 'State Hwy 33 W','State Hwy 29 Bus', 'some 1 other string text with a number','yet another test with a 6 number'] for r in lstRoads: lstr = r.split(' ') for i in range(len(lstr)-1,-1,-1): if lstr.isdigit(): print " - part {0} - in string '{1}' is '{2}' and this is numeric {3}".format(i,r,lstr,float(lstr)) break
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.