1

Suppose I have a field titled NAME1.

Examples of what is in the field appear like this:

Fox Lake 1
WABASCA 166
TSU TUE 196G
YELLOW QUILL I.R. 90-8
POUNDMAKER INDIAN RESERVE NO. 114-17
CROSS LAKE 19X02
BEARDY'S & OKEMASIS I.R. 96 & 97A

How using python or vb script would I extract a string only after the first digit into a new field titled Name_Num?

Basically, the Name_Num field would have to appear like this:

1
166
196G
90-8
114-17
19X02
96 & 97A

Mind you, I need the letter attached to the numbers. I can't just extract digits only. But anything before the first number must go. The numbers vary in character length but always appear at the end of my string.

I rarely use python and am not familiar with when something goes into the pre-code block or doesn't.

These are the results after inputting the first reply which looked great in my opinion but didn't work unfortunately.

results after first comment

1

Assuming that you are looking for positive numbers, as it looks like you are extracting address elements.

Run a field calculate on Name_Num field.

Put this in the pre-logic Script Code (make sure Parser is Python):

def extract_from_number_to_end(input_string):
    output = input_string # return input if no number found
    for char in input_string:
        if char.isdigit():
            pos = input_string.find(char)
            output = input_string[pos:]
            break #stop as we have found the first number
    return output

And call it in the Name_Num = part like:

extract_from_number_to_end(!MyField!)

It defaults to returning the original string if no number is found.

  • Thanks for the quick reply! it didn't work unfortunately, but see image attached to original post. Does that help? – pistolpunz Jun 12 '15 at 23:00
  • Whoops, totally my fault, I forgot to update the calling code to match the name of the def. Have updated my answer, try that. – spg Jun 12 '15 at 23:08
  • Actually, i previously wrote a comment saying it didn't work again. BUT IT DID!!! Only for half the number of records and I don't know why. Results say that there a row containing a bad value. Is there any way to locate this bad value row? – pistolpunz Jun 15 '15 at 16:33
  • Nevermind I realized that one of the entries was too long to fit the 50 char limit, which halted the process. You are a genius. THANK YOU. – pistolpunz Jun 15 '15 at 16:35
  • Sweet, glad I could help! – spg Jun 16 '15 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.