Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60082
| References | <bcd7481a-45c2-4dbb-a9e3-c5faa80ac899@googlegroups.com> <e3614644-c3fe-46e6-b5f1-7f285f1e81b1@googlegroups.com> <CAHWX4tEMGiTYYRKyXOAFg-aZxeZYhh0vH-fAE8vxes=kbc5h3w@mail.gmail.com> |
|---|---|
| Date | 2013-11-20 09:48 -0500 |
| Subject | Fwd: parsing RSS XML feed for item value |
| From | Neil Cerutti <mr.cerutti@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2963.1384958939.18130.python-list@python.org> (permalink) |
Larry Wilson itdlw1@gmail.com via python.org
10:39 PM (10 hours ago) wrote:
>
> Wanting to parse out the the temperature value in the
> "<w:current" element, just after the guid element using
> ElementTree or xml.sax.
Since you aren't building up a complex data structure, xml.sax
will be an OK choice.
Here's a quick and dirty job:
import io
import xml.sax as sax
the_xml = io.StringIO("""SNIPPED XML""")
class WeatherHandler(sax.handler.ContentHandler):
def startDocument(self):
self.temperatures = []
def startElement(self, name, attrs):
if name == 'w:current': # Nice namespace handling, eh?
self.temperatures.append(attrs)
handler = WeatherHandler()
sax.parse(the_xml, handler)
for temp in handler.temperatures:
for key, val in temp.items():
print("{}: {}".format(key, val))
Output (from your example):
windGusts: 29.6
dewPoint: 18.6
pressure: 0.0
windDirection: SSW
humidity: 90
rain: 0.6
temperature: 20.3
windSpeed: 22.2
For most jobs you would want to keep track of your nesting level, but
that's left out here. I didn't try to capture location or info you
might want but didn't specify, either; left that as an exercise.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
parsing RSS XML feed for item value Larry Wilson <itdlw1@gmail.com> - 2013-11-19 19:39 -0800
Re: parsing RSS XML feed for item value xDog Walker <thudfoo@gmail.com> - 2013-11-19 20:06 -0800
Re: parsing RSS XML feed for item value Larry Wilson <itdlw1@gmail.com> - 2013-11-20 05:44 -0800
Fwd: parsing RSS XML feed for item value Neil Cerutti <mr.cerutti@gmail.com> - 2013-11-20 09:48 -0500
Re: parsing RSS XML feed for item value xDog Walker <thudfoo@gmail.com> - 2013-11-20 08:17 -0800
Re: parsing RSS XML feed for item value xDog Walker <thudfoo@gmail.com> - 2013-11-20 08:31 -0800
Re: parsing RSS XML feed for item value Larry Wilson <itdlw1@gmail.com> - 2013-11-20 15:44 -0800
csiph-web