Help with building script for multiple replacements in field

1808
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
RebeccaStrauch__GISP
MVP Emeritus

tagging https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=7b113f9c-55...‌ since I assume you are wanting this in a python script (if not, make sure to specify what you want the script to be written in, or ifyou are just trying in the field calculator box)

MitchHolley1
MVP Regular Contributor

The following should work.

#complete path to .shp or feature class
dataset = r'path_to_dataset'

with arcpy.da.UpdateCursor(dataset, ['OPERATOR']) as cursor:
    for row in cursor:
        if 'ANADARKO' in row[0]:
            row[0] = 'ANADARKO'
        elif 'CHESAPEAKE' in row[0]: #use same elif loop for more companies
            row[0] = 'CHESAPEAKE'
        cursor.updateRow(row)
del cursor
JakeSkinner
Esri Esteemed Contributor

Hi Melissa,

You can do this using the Field Calculator.  Here is the code:

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

Example:

!Company! is the field name you are updating.  In this case, the field is called Company.

MelissaTrascher
New Contributor II

Thank you for your help.  I am still having issues.

0 Kudos
DanPatterson_Retired
MVP Emeritus

You might have indentation issues or anything... 

what is the exact error message?

perhaps you can copy and paste the field calculator expression you are using or at least provide a screen grab.

Maybe jskinner-esristaff‌ could do the same with his expression and attach it as a *.cal file to obviate any indentation/copy issues.

XanderBakker
Esri Esteemed Contributor
def CorrectNames(name):
    dct = {"NADARKO E & P ONSHORE LLC":"ANADARKO",
        "ANADARKO E&P ONSHORE":"ANADARKO",
        "ANADARKO PET":"ANADARKO",
        "ANADARKO PET CORP":"ANADARKO",
        "ANADARKO PETROLEUM CORPORATION":"ANADARKO",
        "CHESAPEAKE LIMITED PARTNERSHIP":"CHESAPEAKE",
        "CHESAPEAKE OPERATING INC.":"CHESAPEAKE",
        "CHESAPEAKE OPERATING INCORPORATED":"CHESAPEAKE",
        "CHESAPEAKE OPERATING, INC.":"CHESAPEAKE",
        "CHESAPEAKE OPERG INC":"CHESAPEAKE"}

    if name in dct:
        return dct[name]
    else:
        return name

I would use a dictionary...

MelissaTrascher
New Contributor II

def calc( !OPERATOR! 😞
  if 'ANADARKO' in field:
    return 'ANADARKO'
  elif 'CHESAPEAKE' in field:
    return 'CHESAPEAKE'

calc( !OPERATOR!)

0 Kudos
MelissaTrascher
New Contributor II

My screenshots are being moderated

0 Kudos
DanPatterson_Retired
MVP Emeritus

change this

def calc( !OPERATOR! 😞

to 

def calc( field):

0 Kudos