Help with building script for multiple replacements in field

1919
21
12-07-2017 10:45 AM
MelissaTrascher
New Contributor II

I have a field that has multiple names for the same company, for several companies.  I would like to build a script to replace the name for each company...

example: replace( [OPERATOR],"ANADARKO E & P ONSHORE LLC","ANADARKO")

FindReplace
ANADARKO E & P ONSHORE LLCANADARKO
ANADARKO E&P ONSHOREANADARKO
ANADARKO PETANADARKO
ANADARKO PET CORPANADARKO
ANADARKO PETROLEUM CORPORATIONANADARKO
CHESAPEAKE LIMITED PARTNERSHIPCHESAPEAKE
CHESAPEAKE OPERATING INC.CHESAPEAKE
CHESAPEAKE OPERATING INCORPORATEDCHESAPEAKE
CHESAPEAKE OPERATING, INC.CHESAPEAKE
CHESAPEAKE OPERG INCCHESAPEAKE

How can I build a single script for all replacements?

Thanks!

0 Kudos
21 Replies
MelissaTrascher
New Contributor II

def calc(field):
  if 'ANADARKO' in field:
    return 'ANADARKO'
  elif 'CHESAPEAKE' in field:
    return 'CHESAPEAKE'

OPERATOR=

calc( !OPERATOR!)

0 Kudos
MelissaTrascher
New Contributor II

Still failed. I am having no luck.

0 Kudos
DanPatterson_Retired
MVP Emeritus

it appears you are trying to calculate values for an 'OPERATOR' field... if so, you can't use values that are already in that field.  You need to make a new field of the same data type... say ... 'NEW_OP'... then you make that field active and use the calculator expression

cal(!NEW_OP!)

of course this assumes that your indentation is totally correct etc etc

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Melissa,

Can you upload a zipped sample of your data?

0 Kudos
MelissaTrascher
New Contributor II

I'm sorry, I'm new here - where do I go to upload data?

0 Kudos
JakeSkinner
Esri Esteemed Contributor

When replying click on Use Advanced Editor at the top right:

Then you will see the Attach option in the lower left hand corner:

MelissaTrascher
New Contributor II

Thanks, I had to reply to original message to see the advanced option.

0 Kudos
MelissaTrascher
New Contributor II

Here is the data I am struggling with.  

0 Kudos
DanPatterson_Retired
MVP Emeritus

Did you try the suggestion https://community.esri.com/thread/206396-help-with-building-script-for-multiple-replacements-in-fiel... because your haven't clarified the name of the field you have the data in and whether or not you are trying to add this to a field of the correct type.  You calculator expression was incorrect as it was posted.

BTW, it appears to have been answered... so what was the solution?

0 Kudos
MelissaTrascher
New Contributor II

I did try it.  I added a text field named it NewOp, selected it, then ran field calc.  It didn't work.  Its time I took a scripting class... the basics are not working in my new job.

I didn't report that this was answered... unless it was by accident when trying to find the advanced editor. Ugh.

0 Kudos