2

Working on ArcMap Desktop 10.5; the file in question lives in a file gdb. I have a polygon layer with a field of full property addresses (string) from which I need to extract ONLY the unit number, to be populated in a new blank field STUNIT (string). The addresses are all different lengths depending on presence of street direction, etc. so my addresses, for example, could look something like this:

1 BOXER ST UNIT 7A FARMINGTON

945 WILD TURKEY XING UNIT 116 TURTLE ISLAND

52 W CLAIRMONT DR UNIT J17 EASTHAVEN

I have already performed a selection to return only records which contain the word "UNIT", about 4000 records. My goal is to populate my STUNIT field using the python parser in field calculator, with ONLY the "word" directly following UNIT. I have tried variations of the split function which looks something like this

!AddressField!.split("UNIT")[0]

but, without fail, get the following error messages:

  • Whitespace is not allowed at this location
  • This row contains a bad value (for both the shapefile itself and the field I tried to populate)

I have checked for spaces within/outside of the function but none exist. I have also tried a variant where I use find and replace to add a * to the end of the word "UNIT" and use the * as the delimiter in the function, but get identical results. Can a word in a string field be used as a delimiter for the split function in Arcpy? If so, what am I doing wrong/if not, is there another workaround that does not require a return on the nth object?

I have several years experience with ArcGIS but relatively new to python.

1
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format.
    – Midavalo
    Commented Jun 16, 2017 at 18:32

2 Answers 2

3

You can do this in the field calculator using regex. Finds the word UNIT and returns the next word. If it doesn't find UNIT then it won't return anything.

In the Field Calculator check Python parser, and Show Codeblock. In the Pre-logic script code enter:

import re
def nextword(s):
    m = re.search('\\UNIT (\w+)', s)
    if m:
        return m.group(1)

And in the expression:

nextword(!AddressField!)

enter image description here

2
  • After hours of research, this method worked beautifully. Thank you so much! I'm surprised that this is not a more commonly discussed problem in GIS addressing.
    – amarshall
    Commented Jun 16, 2017 at 18:54
  • @amarshall Addresses are discussed fairly regularly both here and on Stack Overflow. Everyone stores and deals with addresses differently, and often people run into problems like yours when processes need to change and addresses have to be repackaged in a different way to make it work.
    – Midavalo
    Commented Jun 16, 2017 at 18:56
3

You may want to define a function for this because it will go beyond basic manipulation of the field value.

enable the show codeblock button in field caluclator and in the the pre-logic script code area try this:

def split(AddrField):
    target = ((AddrField.split("UNIT")[1]).split(" "))[1]
    return target

then in field cal area below pass the field as the function's parameter:

split(!AddressField!)
13
  • 2
    That may be the case now, but for future users it would eliminate the need to pre-select anything. +1 anyway, tidier than mine and doesn't require re (I'm not a fan of regex)
    – Midavalo
    Commented Jun 16, 2017 at 18:37
  • 1
    Thank you, atxgis. I ended up using the solution presented by Midavalo, but just for training sake, can you help me understand why your solution didn't work with my data? I pasted your code in exactly as written, but changed instances of "AddressField" to the name of my actual address field. I got the following errors: -Error running expression: split (-->for one specific address) -Traceback (most recent call last): -File "<expression>, line 1, in <module> -File "<string>", line 2, in split -Index Error: list index out of range
    – amarshall
    Commented Jun 16, 2017 at 19:00
  • 1
    @amarshall Also note that python is case picky - UNIT is different from unit and Unit
    – Midavalo
    Commented Jun 16, 2017 at 19:04
  • 1
    well, its hard to say without looking at the data, but a "list index out of range" error means that there was not a result from the split.
    – atxgis
    Commented Jun 16, 2017 at 19:29
  • 1
    @amarshall If you select just one or two records does it work?
    – Midavalo
    Commented Jun 16, 2017 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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