Discuss / Python / 练习

练习

Topic source

Yoz_Hollow

#1 Created at ... [Delete] [Delete and Lock User]
class WeatherSaxHandler(object):
    w={}
    def start_element(self, name, attrs):
        if name=='yweather:location':
            self.w['city']=attrs['city']
            self.w['country']=attrs['country']
        if name=='yweather:forecast':
            if not 'today' in self.w:
                self.w['today']={}
                self.w['today']['text']=attrs['text']
                self.w['today']['low']=int(attrs['low'])
                self.w['today']['high']=int(attrs['high'])
            elif not 'tomorrow' in self.w:
                self.w['tomorrow']={}
                self.w['tomorrow']['text']=attrs['text']
                self.w['tomorrow']['low']=int(attrs['low'])
                self.w['tomorrow']['high']=int(attrs['high'])


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

  • 1

Reply