Remove Alph and special characters from string filed

3140
10
08-31-2016 11:51 AM
2Quiker
Occasional Contributor II

I have a table with a field called group_code and in this field there strings like the following.

2,10,12,15H, 26P, 35$#. As you can see some have a alph and special characters and some don't. So i don't think i can use the cursor.updateRow([row[0][1:-1]]).

I need to remove all the Alph and special characters at the end of each column for the group_code field.

I have tried the following but no luck. I need this to be outside the field calculator.

table = r"C:\Temp\Default.gdb\LandVal"fieldValue = "group_code"  
#Removes alpha at the end  for field in arcpy.ListFields(table, "group_code", "String"):
       exp = ''.join([i for i in fieldValue if i.isdigit()])      
arcpy.management.CalculateField(table, field, exp.format(field), "PYTHON")         
arcpy.conversion.TableToTable(table, r"C:\Temp", "LandVal_Test.dbf")‍‍‍
0 Kudos
10 Replies
ClintonDow1
Occasional Contributor II

Cursors can replace the CalculateField call to make it a bit clearer. 

with arcpy.da.UpdateCursor(file, ['group_code']) as cursor:
   for row in cursor:

      s = row[0]

      row[0] = "".join([c for c in s if c.isnumeric()])

      cursor.updateRow(row)

edit: forgot the updateRow call