Select to view content in your preferred language

Remove first 2 characters using if statement

1940
11
Jump to solution
08-09-2013 02:54 PM
AmyKlug
Frequent Contributor
Hi, I would like to have this code remove and XY or RR (if they are the first 2 characters) in a field with values

XY2365541
RR1247884
8965445

would be:

2365541
1247884
8965445

Writing a query is giving me trouble. I was trying this code with a search cursor, getvalue and if elif statement...but no luck

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
Tags (2)
0 Kudos
11 Replies
T__WayneWhitley
Honored Contributor
That's great Amy!

And just to go a little further because I think I steered you a wee bit wrong in post 4, I said this was 'wrong' but now I don't think so (except you needed the 'if' statement of course):

cursor.updateRow([row[0][2:]])

Actually, I think your code could be shortened to almost what you had originally, see the following, if I can get the formatting preserved from your last post:
>>> import arcpy
>>> fc = r"H:/Test.gdb/XY_1_1"
>>> cursor = arcpy.da.UpdateCursor(fc, ["NM"])
>>> prefix = ["XY", "RR"]
>>> for row in cursor:
...     if row[0][:2] in prefix: 
...         cursor.updateRow([row[0][2:]])
...     else:    
...         print "Name is up-to-date"


...where 'row[0]' is your field object from the list that was previously specified as row = row[0].  Just this line was removed....I didn't test this, but pretty certain it works.  I'm just saying you should be able to do it either way.

Hope that makes sense....I'm more accustomed 'seeing' it the other way and actually you should probably make it however is most readable to you (or your workmates, if they see the code).

Enjoy,
Wayne


EDIT:
That's very interesting - and the code I posted may not work!  (That's what you get when you post code you have not tested, lol!)

In the Method Overview for the da (data access) module for the UpdateCursor, I only see 'updateRow(row)' where row must be the row object of the cursor -- so if the above code does not work (sorry) or if having trouble, you could try this (I cannot test this da update cursor method at the moment):
>>> import arcpy
>>> fc = r"H:/Test.gdb/XY_1_1"
>>> cursor = arcpy.da.UpdateCursor(fc, ["NM"])
>>> prefix = ["XY", "RR"]
>>> for row in cursor:
...     if row[0][:2] in prefix: 
...         row = row[0][2:]
...         cursor.updateRow(row)
...     else:    
...         print "Name is up-to-date"
0 Kudos
by Anonymous User
Not applicable
If you just want to extract the digits and ignore everything else, why not make it simple:

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


This will yield:

2365541
1247884
8965445


EDIT: I should clarify that this removes ALL text and leaves ONLY the digits.  If you want to keep other prefixes use Dan or Wayne's method.
0 Kudos