Question: ```
def ints(filename):
    a = []
    f = open(filename, "r")
    lines = f.readlines()
    f.close()
    for line in lines:
        numbers = line.split()
        for number in numbers:            
            a.append(int(number))
    return a

```

This is my function so far, I want to be able to read a file containing integers and characters like "x" and "b" etc and return a list of only the integers. At the moment the function can only deal with a file containing integers.

How can I modify this to exclude characters or letters?

Answer: 
`regex` can be helpful here:

A simple example:

```
In [22]: import re

In [23]: strs="121 some 34useless text 56"

In [24]: map(int,re.findall("\d+",strs))
Out[24]: [121, 34, 56]

# or this If you want the individual digits:

In [40]: map(int,re.findall("\d",strs))
Out[40]: [1, 2, 1, 3, 4, 5, 6]

```

For your code this should work:

```
for line in lines:
    numbers = map(int,re.findall("\d+",line))
    a.extend(numbers)

```
Using more modern Python idioms:

```
def ints(filename):
    with open(filename, "r") as f:
        for line in f:
            for number in line.split():
                try:
                    yield int(number)
                except ValueError:
                    pass

a = list(ints("testdata.txt"))
print(a)

```

Basically, try to convert to int, which will raise ValueError if the string is not a decimal number. Catch it and ignore it and carry on.