# Using Field Calculator for turning a string into an integer while removing text...

3301
4
02-24-2016 07:15 AM
MVP Notable Contributor

I feel kind of dumb but I cannot figure out how to make this work.

This should be easy, I think, but field calculator is making it difficult for me. I have one field that has text and numbers. I want to use Field Calculator with Python to just grab those numbers and put them into my Long Integer field.

I tried using the example from Stack Exchange but maybe I am doing it wrong because this example is making it a string?

(this returns nothing in my records)

What am I doing wrong?

1 Solution

Accepted Solutions
MVP Frequent Contributor

You need to check if the list you are building contains any digits because if you return anything with only text it will bomb since the field you are calculating is type Integer.

```def makestr(test):
numlist = []
for s in test:
if s.isdigit():
numlist.append(s)
retlist = ''.join(numlist)
if any(char.isdigit() for char in retlist):
return retlist```
4 Replies
MVP Frequent Contributor

You need to check if the list you are building contains any digits because if you return anything with only text it will bomb since the field you are calculating is type Integer.

```def makestr(test):
numlist = []
for s in test:
if s.isdigit():
numlist.append(s)
retlist = ''.join(numlist)
if any(char.isdigit() for char in retlist):
return retlist```
MVP Notable Contributor

James, that makes sense and I ran the codeblock in Field Calculator with your code, but it didn't return anything. There were no errors though. Is there something I am doing wrong?

The field that I am grabbing the values from is called "Layer" and I am putting the values into "Parcel_Numb".

```def makestr(Layer):
numlist = []
for s in Layer:
if s.isdigit():
numlist.append(s)
retlist = ''.join(numlist)
if any(char.isdigit() for char in retlist):
return retlist```

and for the bottom part:

`makestr(!Pacel_Numb!)`

Is it a syntax issue?

MVP Notable Contributor

Ok, it was the bottom part.... I needed to make it say:

`makestr(!Layer!)`

...sheesh...

Thanks James! Again, you're the best!

MVP Esteemed Contributor

Without code blocks, substitute  fld for your field name.

Since you said output is integer, then use:

```>>> fld = "12,ab3c4g-6*"
>>> int("".join([i for i in fld if i.isdigit() ]))
12346
>>>```