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")
Find | Replace |
ANADARKO 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 |
How can I build a single script for all replacements?
Thanks!
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)
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
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.
Thank you for your help. I am still having issues.
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.
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...
def calc( !OPERATOR! 😞
if 'ANADARKO' in field:
return 'ANADARKO'
elif 'CHESAPEAKE' in field:
return 'CHESAPEAKE'
calc( !OPERATOR!)
My screenshots are being moderated
change this
def calc( !OPERATOR! 😞
to
def calc( field):