Select to view content in your preferred language

Custom parse Python string

2040
11
Jump to solution
03-12-2019 02:47 PM
JeremyMcCollum1
Emerging Contributor

I have an attribute field that has a description that I only want to display a specific string or text.  I'm not sure if using a rsearch or rfind or another method for displaying what I want is best? For example, the text field:

 VILLAGES OF WOODLAND SPRINGS BLOCK 44 LOT 4
FIVE OAKS CROSSING ADDN BLOCK 1 LOT 6 PER PLAT D214124070
SUMMER CREEK SOUTH ADDITION BLOCK 8 LOT 3


I am only wanting to pull out the Block and block number and Lot and Lot number

thanks for any advice

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

Jeremy you might want to mark mine correct.

Now for the ones with the spaces, you will have to modify indx0 for those cases

for example...

idx0 = row.index('BL OCK')

View solution in original post

0 Kudos
11 Replies
JoshuaBixby
MVP Esteemed Contributor
>>> import re
... 
... strings = [
...     "VILLAGES OF WOODLAND SPRINGS BLOCK 44 LOT 4",
...     "FIVE OAKS CROSSING ADDN BLOCK 1 LOT 6 PER PLAT D214124070",
...     "SUMMER CREEK SOUTH ADDITION BLOCK 8 LOT 3"
... ]
... 
>>> 
>>> for s in strings:
...     print(re.findall('(BLOCK|LOT) (\S*) ?',s))
...     
[('BLOCK', '44'), ('LOT', '4')]
[('BLOCK', '1'), ('LOT', '6')]
[('BLOCK', '8'), ('LOT', '3')]
>>> 
DanPatterson_Retired
MVP Emeritus

A fake field and a function to try with the field calculator, python parser,

code block is below.

expression line  

do_stuff(!YourFieldNameHere!) 

lines 14-16 simulate a fake column 'a'

hopefully you get the drift. I wrote it as overtly as possible

a = [ 'VILLAGES OF WOODLAND SPRINGS BLOCK 44 LOT 4',
'FIVE OAKS CROSSING ADDN BLOCK 1 LOT 6 PER PLAT D214124070',
'SUMMER CREEK SOUTH ADDITION BLOCK 8 LOT 3']

def do_stuff(a_val):
    """call using do_stuff(!Your_fld!)"""
    row = a_val.split(" ")
    idx0 = row.index('BLOCK')
    idx1 = row.index('LOT')
    out = " ".join(row[idx1:idx1+2] + row[idx0:idx0+2])
    return out
    

for a_val in a:
    returned = do_stuff(a_val)
    print(returned)
    
LOT 4 BLOCK 44
LOT 6 BLOCK 1
LOT 3 BLOCK 8
JeremyMcCollum1
Emerging Contributor

My example that I used actually has lots and lots of blocks and lots with numbers, what could I use to grab them all?

0 Kudos
DanPatterson_Retired
MVP Emeritus

show an example. 

Is the stuff in a table (see the field calculator example I posted) or what format are they in?

0 Kudos
JeremyMcCollum1
Emerging Contributor

0 Kudos
DanPatterson_Retired
MVP Emeritus

add a new text field and try the field calculator code block and expression I posted.  python parser code block as above and the expression would be

do_stuff(!legaldesc!)

0 Kudos
JeremyMcCollum1
Emerging Contributor

Can I see how you put this in?  I can't get mine to work.  I am also doing this in ArgGIS Pro, not sure if that will make a difference

0 Kudos
JeremyMcCollum1
Emerging Contributor

legaldesc is the attribute field that I am looking to do this on

0 Kudos
DanPatterson_Retired
MVP Emeritus

for the visual types...

I swapped the code block up so block appears before lot (my bad in the original)

0 Kudos