import arcpy Feature_Class_Name = arcpy.GetParameterAsText(0) Output_Features = arcpy.GetParameterAsText(1) arcpy.env.workspace = r"H:/TestMDB.gdb/XY_1" cursor = arcpy.da.UpdateCursor(Feature_Class_Name, ["NM"]) for row in cursor: cursor.updateRow([row[0][2:]]) del fc
Solved! Go to Solution.
>>> with arcpy.da.UpdateCursor("Stream_points",['TEST']) as rows: ... for row in rows: ... v = row[0] ... row[0] = ''.join(i for i in v if i.isdigit()) ... rows.updateRow(row) ... >>>
is pseudo code
avalue (a row value in string format)
prefix = ["XY", "RR"]
if avalue[0:2] in prefix:
avalue = avalue[2:]
print avalue
Dan said you "here's your pseudocode", so you have to test something out -- this line is wrong:
cursor.updateRow([row[0][2:]])
...believe you have to set the row field val 1st then update the row...as in something like:
row[0] = row[0][2:]
cursor.updateRow(row)
Try that substitution...actually, I think you specified you wanted to test for the 1st 2 char, then substitute something else - so you'll need an 'if' statement, plenty of examples in the web help. Make a copy of your fc, and start experimenting, following the above for starters.
Enjoy,
Wayne
>>> bunchOfValues = ('XY885442554', 'TY3485694873', 'RR473892711') >>> print bunchOfValues ('XY885442554', 'TY3485694873', 'RR473892711') >>> >>> for eachValue in bunchOfValues: print eachValue XY885442554 TY3485694873 RR473892711 >>> >>> for eachValue in bunchOfValues: if eachValue[0:2] in ['XY', 'RR']: print eachValue[2:] else: print eachValue 885442554 TY3485694873 473892711 >>>
>>> import arcpy >>> fc = r"H:/Test.gdb/NameTest" >>> cursor = arcpy.da.UpdateCursor(fc, ["NM"]) >>> for row in cursor: ... row = row[0] ... if row[:2] == "XY": ... cursor.updateRow([row[2:]]) ... elif row[:2] == "RR": ... cursor.updateRow([row[2:]]) ... else: ... print "Name field is alread up-to-date" >>>> del fc, cursor
>>> bunchOfValues = ['XY885442554', 'TY3485694873', 'RR473892711'] >>> for aValue in bunchOfValues: ... if aValue[:2] in ['XY', 'RR']: ... print aValue[2:] ... else: ... print aValue ... 885442554 TY3485694873 473892711
To emphasize the useage of the "in" list operand consider the following:>>> bunchOfValues = ['XY885442554', 'TY3485694873', 'RR473892711'] >>> for aValue in bunchOfValues: ... if aValue[:2] in ['XY', 'RR']: ... print aValue[2:] ... else: ... print aValue ... 885442554 TY3485694873 473892711
particularly useful to avoid extensive if, elif, else statements
>>> import arcpy >>> fc = r"H:/Test.gdb/XY_1_1" >>> cursor = arcpy.da.UpdateCursor(fc, ["NM"]) >>> prefix = ["XY", "RR"] >>> for row in cursor: ... row = row[0] ... if row[:2] in prefix: ... cursor.updateRow([row[2:]]) ... else: ... print "Name is up-to-date"