Remove Alph and special characters from string filed

2211
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
RandyBurton
MVP Regular Contributor

Try without the square brackets, something like:

>>> string = "35$#"
>>> ''.join(c for c in string if c in '0123456789')
'35'
>>> ''.join(c for c in string if c.isdigit())
'35'
DanPatterson_Retired
MVP Esteemed Contributor

many ways, the only difference between your line 4 and my line 2, is when the join gets done, it is sometimes just a matter of preference.

c = "35$#"
''.join([i for i in c if i.isdigit()])
"35"
2Quiker
Occasional Contributor II

Maybe i didn't explain what i am needing correctly.

working table looks like the following.

OIDgroup_code
135$#
212
326P
410
52

From what i understand is that c = "35$#' is the character to be checked. i don't just need it to check 35$# i need it to check the whole group_code field and remove the the Alph & Special Characters from all of the attributes in the "group_code" field.

Like the following..

OIDgroup_code
135
212
326
410
52

Or maybe i am not understand how to incorporate what you two suggested in to my current script.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Calculate Field—Help | ArcGIS for Desktop 

Check the syntax and use Python_9.3

what did it print? (through in some print statements prior to calculate field)

did you just try to put the field name in explicitly  as the expression ?  ie

''.join([i for i in !FieldName! if i.isdigit()])

0 Kudos
2Quiker
Occasional Contributor II

what i am working with .

#Removes alpha at the end  

import string, arcpy

table = r"C:\Temp\Default.gdb\LV"
for field in arcpy.ListFields(table, "group_code", "String"):
    c = "group_code"
    exp = ''.join([i for i in c if i.isdigit()]) #''.join([i for i in c if i.isdigit()])  
    arcpy.CalculateField_management(table, "group_code", exp.format(field), "PYTHON_9.3")

I am getting error .

ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Expression: Value is required

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

python uses ! marks around the field... don't have arc* here so try !group_code! in place of c... c doesn't need to be a variable In fact give it whirl in the field calculator.  Whatever works there will work in the calculatefield expression... that is how you test them

0 Kudos
RandyBurton
MVP Regular Contributor

I tried the following, which seemed to work.  This is outside ArcMap, but calls the CalculateField tool.

import arcpy
table = r"C:\Temp\Default.gdb\LV"
arcpy.CalculateField_management(table,"group_code","''.join(i for i in !group_code! if i.isdigit())","PYTHON_9.3","#")‍‍‍‍‍‍
DanPatterson_Retired
MVP Esteemed Contributor

glad you tested it Randy... 

0 Kudos
RandyBurton
MVP Regular Contributor

It can also be written:

import arcpy
table = r"C:\Temp\Default.gdb\LV"
field = "group_code"
exp =  "''.join(i for i in !group_code! if i.isdigit())"
arcpy.CalculateField_management(table,field,exp,"PYTHON_9.3")