By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 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
  3.  
  4. #Open folder with files
  5. filelist = glob.glob("/home/clemence/Desktop/testing/*.sae")
  6.  
printing filelist gives me the following:

/home/clemence/Desktop/testing/gal_z0.275.sae'

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


bvdet
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.