Help with building script for multiple replacements in field

275
21
12-07-2017 10:45 AM
Highlighted
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!

Reply
0 Kudos
21 Replies
Highlighted
MVP Esteemed Contributor

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)

Highlighted
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
Highlighted
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.

Highlighted
New Contributor II

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

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

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.

Highlighted
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...

Highlighted
New Contributor II

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

calc( !OPERATOR!)

Reply
0 Kudos
Highlighted
New Contributor II

My screenshots are being moderated

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

change this

def calc( !OPERATOR! 😞

to 

def calc( field):

Reply
0 Kudos