python - Removing part of a string that has been obtained by a regular expression using strip functions -
i having problem. have regular expression looking through rss feed weather
url = 'http://rss.weatherzone.com.au/?u=12994-1285<=aploc&lc=9388&obs=1&fc=1&warn=1' weather_brisbane = urlopen(url) html_code = weather_brisbane.read() weather_brisbane.close()
i have regex:
weather_contents = findall('<b>(.+)</b> (.*)', html_code) if weather_contents != []: print 'contents' section_heading in weather_contents: print section_heading print
i result:
contents ('temperature:', '20.1°c\r') ('feels like:', '20.1°c<br />\r') ('dew point:', '13.6°c\r') ('relative humidity:', '66%<br />\r') ('wind:', 'e @ 2 km/h, gusting 4 km/h\r') ('rain:', '0.0mm since 9am<br />\r') ('pressure:', '1024.9 hpa\r')
so question is, there way result:
contents temperature: 20.1 feels like: 20.1 dew point: 13.6 relative humidity: 66% wind: e @ 2 km/h, gusting 4 km/h rain: 0.0mm since 9am pressure: 1024.9 hpa
by integrating strip() function existing code.
there alternative of htmlparser:
print ' '.join([s.rstrip('\r').rsplit('<br />')[0].rsplit('°c')[0] s in section_heading])
instead of
print section_heading
Comments
Post a Comment