Calculate Fields in Arcade or Python 3

393
2
08-05-2022 08:55 AM
SibGISAdmin
New Contributor

I would like to create a hyperlink to display platbook pages. In order to do this, I must find a way to pull in the values within a paragraph from an existing field, then use the calculate field tool to separate the pages that would each be recorded in to newly created fields.

The problem is that I am not sure how to search for special characters in a paragraph using either ARCADE or PYTHON 3 in ArcGIS Pro. For example, how could I search for a the "-" character within a paragraph to find/separate the 44 and 42 values within the following string "North Miami Beach PB 44-42 LOT SIZE 21815" ?

I was thinking maybe helpers like Split, Left, or Mid would be used. It has been tricky because the values that I would try separate are not always in the same position when doing a character count. 

 

Any insight in to how to get started on this would be helpful. 

Peterson Pierre-Louis
GIS Analyst
Information Technology

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

The basic idea, could be done much shorter, but it is easier to see the steps in full first.

# basically [i.split("-") for i in p.split(" ") if i.count("-") == 1]

# code block
def spl(p, as_int=True):
    """splitter"""
    s = p.split(" ")
    if len(s) == 0:
        return p
    for i in s:
        if i.count("-") == 1:
            l, r = i.split("-")
            if as_int:
                l, r = int(l), int(r)
    return l, r

# expression
# spl(!YourFieldName!)

# example

p = 'North Miami Beach PB 44-22 LOT SIZE 21815'
spl(p)
(44, 22)

... sort of retired...
AndyAnderson
Occasional Contributor II

“Split, Left, or Mid” are Visual Basic/Excel string functions whose simplicity has frustrated me for decades. With either Python or Arcade you have so many better ways to select portions of strings. In this case, I would use regular expressions to match a pattern, e.g. in Python:

import re
string = "North Miami Beach PB 44-22 LOT SIZE 21815"
# match one or more + digits \d on either side of - and save them in two groups ()
match = re.search('(\d+)-(\d+)', string) 
p1 = 
match.group(1)     # '44'
p2 = match.group(2)     # '22'

— Andy