[Tutor] parse text file

Stefan Behnel stefan_ml at behnel.de
Tue Feb 2 13:05:12 CET 2010


Norman Khine, 02.02.2010 10:16:
> get_record = re.compile(r"""openInfoWindowHtml\(.*?\\ticon: myIcon\\n""")
> get_title = re.compile(r"""<strong>(.*)<\/strong>""")
> get_url = re.compile(r"""a href=\"\/(.*)\">En savoir plus""")
> get_latlng = re.compile(r"""GLatLng\((\-?\d+\.\d*)\,\\n\s*(\-?\d+\.\d*)\)""")
> 
> records = get_record.findall(data)
> block_record = []
> for record in records:
> 	namespace = {}
> 	titles = get_title.findall(record)
> 	for title in titles:
> 		namespace['title'] = title

I usually go one step further:

    find_all_titles = re.compile(r"""<strong>(.*)<\/strong>""").findall
    for record in records:
	titles = find_all_titles(record)

Both faster and more readable (as is so common in Python).

Stefan



More information about the Tutor mailing list