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)

```
How about using [string.translate](http://docs.python.org/2/library/string.html#string.translate) etc. to just replaces all non digits with spaces then leverage the power of split() and map().

Granted it's a little obscure and my default response would be to just use the [re](http://docs.python.org/2/library/re.html) module as on the whole there's so much more you can do with Regular Expressions, that they're worth the effort in learning.

```
In [119]: import string
In [120]: allchars = string.maketrans('', '')
In [121]: delchars = allchars.translate(allchars, "0123456789")
In [122]: emptychars = string.maketrans(delchars, ' ' * len(delchars))
In [123]: "Welcome home 1234 56 ol".translate(emptychars)
Out[123]: '             1234 56   '
In [124]: "Welcome home 1234 56 ol".translate(emptychars).split()
Out[124]: ['1234', '56']
In [125]: map(int, "Welcome home 1234 56 ol".translate(emptychars).split())
Out[125]: [1234, 56]

```