Splitting String at ',' accounting for No Data

1400
23
01-10-2019 10:44 AM
deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

I've got a table with a field named Maintenance Tasks. I would like to parse the values in this field into separate fields (Task1, Task2 etc.):

I can use:

         None if !MaintenanceTasks! == None else !MaintenanceTasks!.split(',')[0]

and this will parse the first item correctly into Task1, it also accounts for any null values in MaintenanceTasks.

The problem I'm running into is, for example, OID 23 above, when there's just a single item in the MaintenanceTasks field (ReplaceHead).

Any help would be greatly appreciated. FWIW I'll be scripting in Python.

Tags (1)
0 Kudos
23 Replies
DanPatterson_Retired
MVP Emeritus
a = ['a, b', 'c']

for i in a:    # ---- split works properly, always returning a list
    print(i.split(","))
    
['a', ' b']
['c']

for i in a:    # ---- as evidenced below
    print(i.split(",")[0])
    
a
c

You can always use a code block rather than the 'smokin' one-liner

def splitter(a):
    """ """
    if i is None:
        return None
    else:
        return i.split(',')[0]

# ---- expression
splitter(!YourFieldNameHere!)
JoshuaBixby
MVP Esteemed Contributor

In terms of style, None is a singleton so the idiomatic way to work with it is var is None.  For the Task1 field, I would go with:

!MaintenanceTasks!.split(',')[0] if !MaintenanceTasks! else None

For the Task2 field, I would try:

!MaintenanceTasks!.split(',')[1] if !MaintenanceTasks! and ',' in !MaintenanceTasks! else None‍‍

DanPatterson_Retired
MVP Emeritus

careful...

n = None

"," in n
Traceback (most recent call last):

  File "<ipython-input-22-0746bac53200>", line 1, in <module>
    "," in n

TypeError: argument of type 'NoneType' is not iterable

# ---- BUT str, comes to the rescue

 "," in str(n)

False

# --- amend the smokin' oneliner appropriately
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The first part of the logical check prevents the TypeError:

>>> n = None
>>> n and "," in n
>>> 
>>> n = 'string'
>>> n and "," in n
False
>>> 
>>> n = 'string,'
>>> n and "," in n
True
>>> 
0 Kudos
deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

Thanks Joshua Bixby‌. This gets me to Task2. However, I have Task3, Task4, Task5, Task6 and Task7.

Any thoughts?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Seeing you have several fields, it really is a job better suited for ArcPy and an update cursor rather than the Field Calculator.  That said, if you will only do this once and be done, then maybe a slightly clunky and manual way with the field calculator makes sense instead of learning how to script.  

I believe the following will work, just increment the numbers starting with 0 for each new field:

!MaintenanceTasks!.split(',')[0] if !MaintenanceTasks! and !MaintenanceTasks!.count(',') >= 0 else None

For example, Task1 will use 0, Task2 will use 1, Task3 will use 2, etc....

deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

Thanks Joshua Bixby‌. This will happen monthly, so I'll likely use the DA module / update cursor.

Cheers.

0 Kudos
JoeBorgione
MVP Emeritus

To add to Dan's comment:  are you absolutely, 100% sure that the Maintenance Tasks values are comma separated? You may want to check for that. (Not that I've ever been burned by something like that...)

I also notice that OID 17 has three elements.  Since you can't control how many elements you'll be presented (None through N), you may want to weave a len() method into your approach to determine how many elements you'll be extracting and which subsequent fields you'll be populating.

That should just about do it....
0 Kudos
deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

Thanks Joe, it is a comma - these are coming in from a list of check boxes in Survey123.

And you're right - I have up to 7 tasks in this field, so I'll need to look into the len() method.

Thanks!

0 Kudos