Extracting the Ingredients from a Recipe with Regular Expressions : XML Parse « XML « Python






Extracting the Ingredients from a Recipe with Regular Expressions

 

import sys
import re

pat = r'<ingredients>((.|\n)*?)</ingredients>'
filename = sys.argv[1]

data = open(filename).read()

m = re.search(pat,data)
if not m:
    print "No ingredients found"
    print sys.exit(1)

ingredients = m.group(1)

pat = r'<item\s.*?>(.*?)</item>'
all = re.findall(pat,ingredients)

for item in all:
    print item

   
  








Related examples in the same category

1.Processing XML with Regular Expressions
2.Simple XML Document Analysis
3.Parsing XML from Local Files and Remote URLsParsing XML from Local Files and Remote URLs
4.Parsing XML from a URL
5.Parsing XML from StringsParsing XML from Strings
6.Parsing XML: Loading an XML Document