ModelBuilder - Convert VB Script to Python Coding HELP!!

192
3
Jump to solution
01-22-2020 12:37 PM
deleted-user-A8p9VIOt44Kb
New Contributor

Hi everyone and thank you in advance for all your answers. 

First thing first - I am a beginner in Python and I might ask stupid questions. Hopefully this won't be one of them. 

We have an old tool that works perfectly in ArcMap but recently we had no choice but convert it to PRO. I ran the analyse tool in geoprocessor to open the "old toolbox" in PRO and I can edit the model. After going through all the tools and testing I found that some of the "Calculate Field" tools are in VB. So these need to change it to Python for it to run in PRO. 

In short, the tool needs to find the number (typically "55" or "60" always a double number) in a column/field (let's call it "Address") and populate the "Number" field/column with the number. 

The current strict is as follw:

 Number = 
is_string(!MAX_%Field Name%!)

Code Block

def is_string(s):
   if isinstance(s, basestring) == True:
     num = ''

     for i in s:
       if i in '01234567890':
         num+=i
     integer = int(num)
return integer
if isinstance(s, basestring) == False:
return s

Any help on this would be great thank you.

0 Kudos
1 Solution

Accepted Solutions
JoeBorgione
MVP Esteemed Contributor

If they always have an underscore separating the alpha characters from the numeric characters, you can use the split() function in the field calculator something like this:

NumberFieldName = int(!Address!.split('_')[1]‍‍‍)

That just splits the the value of whatever is in the Address field at the underscore and turns the #1 element (it is 0 based) into an integer.  'ABCD' is element[0]

can't wait to retire....

View solution in original post

3 Replies
JoeBorgione
MVP Esteemed Contributor

In short, the tool needs to find the number (typically "55" or "60" always a double number) in a column/field (let's call it "Address") and populate the "Number" field/column with the number. 

 

Is the Address field a double or text type?  I'm not exactly sure what it is you are trying to do; I'm sure it's not as simple as calculating one field based on the value of another field.  Or is it?

can't wait to retire....
0 Kudos
deleted-user-A8p9VIOt44Kb
New Contributor

The Address field is a Text type. Example

In the Address Field/column, a feature would have the following, "ABCD_55" I need the "55" to populate the other field/column Called "Number".

0 Kudos
JoeBorgione
MVP Esteemed Contributor

If they always have an underscore separating the alpha characters from the numeric characters, you can use the split() function in the field calculator something like this:

NumberFieldName = int(!Address!.split('_')[1]‍‍‍)

That just splits the the value of whatever is in the Address field at the underscore and turns the #1 element (it is 0 based) into an integer.  'ABCD' is element[0]

can't wait to retire....

View solution in original post