[re.finditer] Getting all occurences in one go?

Gilles Ganault nospam at nospam.com
Sun Apr 22 17:28:23 EDT 2007


Hello

I'd like to make sure there isn't an easier way to extract all the
occurences found with re.finditer:

=======================
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req).read()
matches = re.compile("(\d+).html").finditer(response)
# ----------- BEGIN
for match in matches:
	if mytable[item]=="":
		mytable[item]= match.group(1)
	else:
		mytable[item]= mytable[item] + "," + match.group(1) #
----------- END
=======================

Can the lines between BEGIN/END be simplified so I can copy all the
items into the mytable[] dictionary in one go, instead of getting each
one in a row, and append them with a comma, eg.

# I want this : mytable[123] = 6548,8457,etc."
# DOESN' T  WORK
# mytable[item] = matches.group(0)
mytable[item] = matches.getall()

Thank you.



More information about the Python-list mailing list