By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,347 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,347 IT Pros & Developers. It's quick & easy.

Extracting text from alphanumeric strings

P: 1
Hi There.

I'm trying to write a code that reads the filenames of objects of the form "gal_z7.25.sae" and "gal_z0.02563.sae" and stores the decimal digit parts ONLY to a list.

So far I have the following:
Expand|Select|Wrap|Line Numbers
  1. import glob
  2. import sys, os
  4. #Open folder with files
  5. filelist = glob.glob("/home/clemence/Desktop/testing/*.sae")
printing filelist gives me the following:


And each item in filelist contains the numbers I need. All I need is a way of ignoring the text and taking only the numbers into another list.

Please help
Jul 22 '14 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,851
This can be done with a regular expression.
Expand|Select|Wrap|Line Numbers
  1. >>> results = ["gal_z7.25.sae", "gal_z0.02563.sae"]
  2. >>> import re
  3. >>> patt = re.compile(r"[^0-9.]+([0-9]+.[0-9]+)")
  4. >>> for item in results:
  5. ...     print patt.match(item).group(1)
  6. ...     
  7. 7.25
  8. 0.02563
  9. >>> 
Jul 22 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.