Help me refresh my Python memory on how to do this

Alex Martelli aleaxit at yahoo.com
Mon Apr 2 12:29:38 EDT 2001


"Kemp Randy-W18971" <Randy.L.Kemp at motorola.com> wrote in message
news:mailman.986219102.21440.python-list at python.org...
> It's been a while since I worked with Python, so tell me how to do this.
>
> I have a text file, and for simplicity sake, I will only put in two
entries
>
> Min. line width (mils):                 4
> 4.00 mils trace segment count / % of total:    5598/80.10 %
>
> I wish to parse this input file and just output the numbers in a separate
text file, like so
> 4
> 5598
> 80.10
>
> I think I will need the split function, but what is the simplest way to
accomplish this?

Depending on how you want to define "the numbers", something like:

import re
number = re.compile(r'\d+\.?\d*')
rawtext = open('a.txt').read()
for anumber in number.findall(rawtext):
    print anumber

might come close to working, however, if a.txt is your
example file, the output would be:

D:\>python genus.py
4
4.00
5598
80.10

as there is no indication that, out of the four 'numbers'
it finds, it needs to ignore the "4.00" one.  So, if you
only want three 'numbers' from that example, you need to
refine your specs...


Alex






More information about the Python-list mailing list