Discuss / Python / 练习

练习

Topic source

冬凉默殇

#1 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from xml.parsers.expat import ParserCreate

class WeatherSaxHandler(object):

    def __init__(self):
        self.weather = dict()

    def start_element(self, name, attrs):
        if name == "yweather:location":
            self.weather['city'] = attrs['city']
            self.weather['country'] = attrs['country']
        if name == "yweather:forecast":
            if 'today' not in self.weather.keys():
                self.forecast('today', attrs)
            elif 'tomorrow' not in self.weather.keys():
                self.forecast('tomorrow', attrs)

    def forecast(self, date, attrs):
        self.weather[date] = dict()
        self.weather[date]['low'] = int(attrs['low'])
        self.weather[date]['high'] = int(attrs['high'])
        self.weather[date]['text'] = attrs['text']

def parse_weather(xml):
    handler = WeatherSaxHandler()
    parser = ParserCreate()
    parser.StartElementHandler = handler.start_element
    parser.Parse(xml)
    return (handler.weather)

  • 1

Reply