Replace Stand Alone Script

734
7
Jump to solution
07-05-2019 09:36 AM
GIS_geek
New Contributor III

I am trying to remove prefixes from a field, eg. SV192W001 to 192W001.  I run the following code and it runs without any errors but when I look at the field none of the prefixes are removed/replaced.  Any help appreciated.  Thanks in advance.

import arcpy
from arcpy import env

# Set workspace
arcpy.env.workspace = "B:/Projects/Duplicate Assets/Duplicate.gdb/dwSystemValve"

fieldName = "ValveID"

# Remove prefixes from ValveID
print("Removing prefixes")
fieldName.replace("SV","")
fieldName.replace("HV","")
fieldName.replace("SR","")

print("Done")‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Oh, your new code makes it more clear to me what you are trying to do. Here are some expressions that should work.

expression = '!ValveID!.replace("SV","").replace("HV","").replace("SR","").replace("ZB","")'
# or simply remove the first two characters:
expression = '!ValveID![2:]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍'

View solution in original post

7 Replies
DanPatterson_Retired
MVP Emeritus

That script does nothing since the field is never acquired.  It would probably just be faster to use the field calculator to accomplish what you need, however, if you need to do this within a script, then you have to implement

Calculate Field—Data Management toolbox | ArcGIS Desktop 

unless you want to get involved with update cursors

GIS_geek
New Contributor III

I did think using field calculator, but I have more to this script and additional feature classes.  I did implement Calculate Field and it sort of worked.  At the end of the process I looked at my data and only the "ZB" was removed from all values containing it in the ValveID field.  Does the CalculateField_management tool only accept one statement in the expression or does it have the ability to accept multiple statements?  I tried using '&' and ',' instead of 'and' and both returned errors.  Here is my updated code.  Still a newbie to python and trying to get a hang of it.  Thanks for your help.

import arcpy
from arcpy import env

# Set workspace
arcpy.env.workspace = "B:/Projects/Duplicate Assets/Duplicate.gdb"

# Set variables
inTable = "dwValves_copy"
fieldName = "ValveID"
expression = '!ValveID!.replace("SV","")' and '!ValveID!.replace("HV","")' 
and '!ValveID!.replace("SR","")' and '!ValveID!.replace("ZB","")'

# Remove prefixes from ValveID
print("Removing prefixes")
arcpy.CalculateField_management(inTable, fieldName , expression, "PYTHON3")

print("Done")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
curtvprice
MVP Esteemed Contributor

Normally field names cannot be changed but there is a geoprocessing tool for that: Alter Field (only supported in ArcGIS Pro). In ArcMap you need to create a new field, copy the data over using Calculate Field (as Dan suggests) and delete the old field once you have verified the data have been successfully copied.

I should warn you that field names starting with a number can cause you major problems down the road

https://community.esri.com/people/curtvprice/blog/2016/05/07/how-to-name-things-in-arcgis?sr=search&...

0 Kudos
DanPatterson_Retired
MVP Emeritus

Hector, you would need a code block, in calculate field

def r(a):
    """replace the values in the field"""
    if a in ("SV", "HV", "SR", "ZB"):
        return a.replace(a, "")
    return a

your condition wasn't an 'and' but an 'or' since the row value can only be 1 thing

expression section

r(!YourField!)

curtvprice
MVP Esteemed Contributor

Oh, your new code makes it more clear to me what you are trying to do. Here are some expressions that should work.

expression = '!ValveID!.replace("SV","").replace("HV","").replace("SR","").replace("ZB","")'
# or simply remove the first two characters:
expression = '!ValveID![2:]‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍'
GIS_geek
New Contributor III

Thank you all for your help!!

0 Kudos
DanPatterson_Retired
MVP Emeritus
inTable = "dwValves_copy"
fieldName = "ValveID"

code_block = '''
def r(a):
    """replace the values in the field"""
    if a in ("SV", "HV", "SR", "ZB"):
        return a.replace(a, "")
    return a
'''

expression = "r(!ValveID!)"
arcpy.CalculateField_management(inTable, fieldName , expression, "PYTHON3")
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

should you need to add more values to your list of things to replace, just add them to line 7