Gilles Ganault <[EMAIL PROTECTED]> writes: > Hello > > After downloading a web page, I need to search for several patterns, > and if found, extract information and put them into a database. > > To avoid a bunch of "if m", I figured maybe I could use a dictionary > to hold the patterns, and loop through it: > > ====== > pattern = {} > pattern["pattern1"] = ">.+?</td>.+?>(.+?)</td>"
pattern["pattern1"] = re.compile(">.+?</td>.+?>(.+?)</td>") > for key,value in pattern.items(): > response = ">whatever</td>.+?>Blababla</td>" > > #AttributeError: 'str' object has no attribute 'search' > m = key.search(response) m = value.search(response) > if m: > print key + "#" + value > ====== > > Is there a way to use a dictionary this way, or am I stuck with > copy/pasting blocks of "if m:"? But there is no reason why you should use a dictionary; just use a list of key-value pairs: patterns = [ ("pattern1", re.compile(">.+?</td>.+?>(.+?)</td>"), ("pattern2", re.compile("something else"), .... ] for name, pattern in patterns: ... -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list