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")
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'
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"
Maybe i didn't explain what i am needing correctly.
working table looks like the following.
OID | group_code |
---|---|
1 | 35$# |
2 | 12 |
3 | 26P |
4 | 10 |
5 | 2 |
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..
OID | group_code |
---|---|
1 | 35 |
2 | 12 |
3 | 26 |
4 | 10 |
5 | 2 |
Or maybe i am not understand how to incorporate what you two suggested in to my current script.
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()])
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
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
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","#")
glad you tested it Randy...
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")