regular expression to extract text

Marc Boeren M.Boeren at guidance.nl
Thu Nov 20 16:48:12 CET 2003


> What I want to do is pull out the bits of information to
> eventually put in an html table.

I'm just supposing for the moment that each of these 3 lines is already
available as a separate line (or in a list of lines)

> 1.QEXZUO
> 2. C26 H31 N1 O3
> 3. 6.164   15.892   22.551    90.00    90.00    90.00

A simple split on spaces would do the trick I think. As an example, the
third line with the leading '3.' stripped would result in

>>> [v for v in line.split(' ') if v]
['6.164', '15.892', '22.551', '90.00', '90.00', '90.00']

Same thing works for the second line and the first line.

You can strip the leading 1., 2. and 3. (if they are part of your data) with
a limited split on periods:

>>> string.split(".", 1)[1]
' 6.164   15.892   22.551    90.00    90.00    90.00'

(string.split(".", 1)[0] contains the '3')

Cheerio, Marc.






More information about the Python-list mailing list