Trying to make a label Expression that just returns any number in a field, doesnt need to be any specfic data type just tying to get the label to only display numbers so I can put that number on a highway shield.
My python snippet works in one of my maps, but not the other even though they are the same dataset in the same GDB.
Here is my code:
int(''.join(filter(str.isdigit, [FULLNAME])))
The error I receive from the non-working map:
ValueError: invalid literal for int() with base 10: "
The field type is string for both.
You have an entry which can't be converted from text to a number....
FULLNAME = 'A1A' # ---- bad
int(''.join(filter(str.isdigit, [FULLNAME])))
Traceback (most recent call last):
File "<ipython-input-2-29cd7d948fcc>", line 1, in <module>
int(''.join(filter(str.isdigit, [FULLNAME])))
ValueError: invalid literal for int() with base 10: ''
FULLNAME = '100' # ---- good
int(''.join(filter(str.isdigit, [FULLNAME])))
100
I'm sorry, I get it is a stupid question but who else am i suppose to ask?
I don't have a deep knowledge of this but why would it work on one and not the other? There are other values that have numbers and letters that only provide the numbers so why not that one?
It isn't a stupid question, it is a common error which can normally be overcome by breaking the expression down into parts.
FULLNAME = 'A1A'
FULLNAME.isdigit()
False # ---- not all parts of the string are numbers
''.join(filter(str.isdigit, [FULLNAME]))
'' # ---- So it returns nothing
# ---- fails because you can't convert an empty string to an integer
int(''.join(filter(str.isdigit, [FULLNAME])))
Take your time, use a couple of steps, there are no rewards for the sweet one-liner
FULLNAME = 'A1A'
tmp = "".join([i for i in FULLNAME if i.isdigit()])
tmp
'1' # ---- the list comprehension splits the parts of the string
# ---- now, the check again, if it can be converted to an integer
# return it, otherwise a suitable nodata value
int(tmp) if tmp.isdigit() else -999
1 # ---- success
# ----- Now test with a no-number highway
FULLNAME = 'A_one_A'
tmp = "".join([i for i in FULLNAME if i.isdigit()])
int(tmp) if tmp.isdigit() else -999
-999
Break things into bits, I do it all the time