Extracting values from text file

Ant antroy at gmail.com
Fri Jun 16 07:45:35 EDT 2006


> What I first though was if there was possible to make a filter such as:
>
>   Apples (apples)
>   (ducks) Ducks
>   (butter) g butter

Try something like:

import re

text = """> Some text that can span some lines.

   Apples 34
   56 Ducks

 Some more text.

"""

filters = {"apples": re.compile(r"Apples\s+(\d+)"),
             "ducks": re.compile(r"(\d+)\s+Ducks"),
             "butter": re.compile(r"([0-9.]+)\s+g\s+butter")}

out = []

for k,v in filters.iteritems():
    matches = v.findall(text)
    for match in matches:
        out.append((k, match))

print out




More information about the Python-list mailing list