Splitting String at ',' accounting for No Data

1404
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
deleted-user-qpvAI3Fo0MKR
Occasional Contributor III

Just to add to my earlier reply - I thing the .count(",")+1 method might be a better idea, no?

0 Kudos
JoeBorgione
MVP Emeritus

I'm a list guy; gimme a list in python and I'll clean the plastic out of the oceans and get the furloughed fed works a pay check...  All seriousness aside:

for i in range(len(someList)):
  do you thing...‍‍‍‍

 Personal preference is all....

I like Joshua's suggestion of the using a da.UpdateCursor too....

That should just about do it....
JoshuaBixby
MVP Esteemed Contributor

Joe, have you seen Ned Batchelder: Loop Like A Native ?  I think you will find it worth the watch.  Ned does a good job of showing how certain common patterns in other languages, e.g., for i in range(len(someList)), can be rewritten to be much more Pythonic.

JoeBorgione
MVP Emeritus

I'll check it out....  thanx

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

enumerate...

try not to use …. range(len(something))

python linters will nail your code every time, and when you get used to it, it gives more and more possibilities.

good reading for the weekend Joe

JoeBorgione
MVP Emeritus

cool...  just tried it.....

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

Can you elaborate on this comment a bit, please? I'm pretty familiar with ArcPy, but this sounds more fundamental...

0 Kudos
JoeBorgione
MVP Emeritus

I won't steal any of Dan's thunder, but take a look here:  13. Enumerate — Python Tips 0.1 documentation . It's an alternative to what I suggested.  The link Joshua provides is very good as well!

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I had to watch it a couple times, maybe a few, to grasp all of the points he was raising.  I watched it years ago, but it really did help me start to look at iterating/iterables/sequences in a different way than other languages.  Python is awesome.

0 Kudos
JoeBorgione
MVP Emeritus

Skimmed it yesterday at work.  Watched it again this morning with coffee...  Gonna need a few more iterations of watching it and using the examples... (all puns intended.)

That should just about do it....
0 Kudos