How to extract numbers from filename in Python?

you can use regular expressions:

regex = re.compile(r'\d+')

Then to get the strings that match:

regex.findall(filename)

This will return a list of strings which contain the numbers. If you actually want integers, you could use int:

[int(x) for x in regex.findall(filename)]

If there's only 1 number in each filename, you could use regex.search(filename).group(0) (if you're certain that it will produce a match). If no match is found, the above line will produce a AttributeError saying that NoneType has not attribute group.


If there is just one number:

filter(lambda x: x.isdigit(), filename)

So, you haven't left any description of where these files are and how you're getting them, but I assume you'd get the filenames using the os module.

As for getting the numbers out of the names, you'd be best off using regular expressions with re, something like this:

import re
def get_numbers_from_filename(filename):
    return re.search(r'\d+', filename).group(0)

Then, to include that in a for loop, you'd run that function on each filename:

for filename in os.listdir(myfiledirectory):
   print get_numbers_from_filename(filename)

or something along those lines.

Tags:

Python