Select to view content in your preferred language

Remove first 2 characters using if statement

1815
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
1 Solution

Accepted Solutions
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.

View solution in original post

0 Kudos
11 Replies
DanPatterson_Retired
MVP Emeritus
is pseudo code
avalue (a row value in string format)
prefix = ["XY", "RR"]
if avalue[0:2] in prefix:
   avalue = avalue[2:]
print avalue
0 Kudos
AmyKlug
Frequent Contributor
is pseudo code
avalue (a row value in string format)
prefix = ["XY", "RR"]
if avalue[0:2] in prefix:
   avalue = avalue[2:]
print avalue


not really sure what to do here
0 Kudos
T__WayneWhitley
Honored Contributor
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
0 Kudos
AmyKlug
Frequent Contributor
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




I will be more specific......

is pseudo code
avalue (a row value in string format)
prefix = ["XY", "RR"]
if avalue[0:2] in prefix:
avalue = avalue[2:]
print avalue

I read this as.......if row[0:2] in ["XY", "RR].........this makes no sense to me. Row[0:2] is a row value such as XY885442554 and nothing I have tried recognizes ["XY, "RR"]. Do I need to also use a search cursor?
0 Kudos
DanPatterson_Retired
MVP Emeritus
Within a cursor of some form (insert possibly in your case), if the first two characters ( [0:2] ) are "XY" or "RR" are found within the offending list ["XY", "RR"], substitute/update your field/row with the value with those values removed.

It is hard to debug someone else's code unless you have field calculator syntax that works before you move on to automating the process to include multiple cases.  Try to simply the operation by performing single steps at a time (eg.  strip off "XY" if it is contained within a field) then move onto the next selection finding fields that begin with "RR" and stripping those off.  If this is a one-off deal you are done...if this is daily, then generate a field calculator expression which covers all cases.   I often find the former faster than the latter and I have been coding for years.
0 Kudos
T__WayneWhitley
Honored Contributor
Yes, maybe the 'stumbling block' here is the isolation of text to trim, or at least selecting those for some kind of conditional 'slicing' or text formatting, whatever -- so I have found the best way to grow more accustomed to how Python handles string handling is to test something interactively, say in IDLE or ArcMap's python window.  You can even test expressions in the field calculator - as long as you have saved a copy of your data, you can practice, no matter really if it succeeds/fails or even crashes/freezes ArcMap, think we've all done that...restart and try again.

Having said that, check out this behavior using IDLE:
>>> 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
>>> 



...still want more, see this, the skinny:
http://stackoverflow.com/questions/509211/the-python-slice-notation
0 Kudos
AmyKlug
Frequent Contributor
Thanks Dan and Wayne.....that really helped. Here is the final code:

>>> 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
0 Kudos
DanPatterson_Retired
MVP Emeritus
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
0 Kudos
AmyKlug
Frequent Contributor
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



True! Here is the code:

>>> 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"


Thanks again
0 Kudos