Populating Attributes via python - All values after a key word

501
1
Jump to solution
12-27-2022 09:09 AM
MichaelDeliberato
New Contributor III

Good Morning All, 

I've been tasked with populating a unit (Text Field) attribute in a specific data set. All units in this data are currently blank, BUT inside of the Unit Description field, there is a unit number, that in 99 % of the cases, follows the word "UNIT". So my question is this. Is it possible to create a python script to find the word "UNIT" in the Unit description field, Then populate the Unit field with all characters that follow that word? Thank you in advance for your responses and help

-MD-

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

many ways, this is but one

fld ="I am UNIT 99A b c"  # -- a sample field entry
(fld.rpartition("UNIT")[-1]).strip()  # -- python parser of course
'99A b c'  # -- result

This partitions a text string based on a separator.  [-1] takes the last bit of the partition.  strip() just removes any leading or trailing spaces.

replace fld in the above with your fieldname enclosed in exclamation marks

eg !UnitDescription!

 


... sort of retired...

View solution in original post

1 Reply
DanPatterson
MVP Esteemed Contributor

many ways, this is but one

fld ="I am UNIT 99A b c"  # -- a sample field entry
(fld.rpartition("UNIT")[-1]).strip()  # -- python parser of course
'99A b c'  # -- result

This partitions a text string based on a separator.  [-1] takes the last bit of the partition.  strip() just removes any leading or trailing spaces.

replace fld in the above with your fieldname enclosed in exclamation marks

eg !UnitDescription!

 


... sort of retired...