Python interaction design based on Dragonbaord 410c development board (3) - weather forecast xml data analysis

I have already introduced how to design the interface and get the weather data from the network weather API interface in the design weather forecast demo. However, in actual application, we may only need a very small part of the weather data, and can see this part of the data extracted. A separate display or application, such as in your DIY you may only need to display some index prompts in the interface, or just need a simple weather status, etc., then we need to parse the acquired data, today's blog Lieutenant General will further teach you how to analyze the weather data in the obtained XML format.

The following figure is the weather XML data format. In fact, the syntax of XML is very simple. It consists mainly of tags and content. The tags are composed of start tags and end tags, and can be nested. In the process of parsing, we usually pass Determine whether it is the starting tag, then read in the data, and use the loop or recursive method to complete the parsing of xml.

At present, for the parsing of XML, there are already many component interfaces that can be called directly, without having to go to the bottom-level character matching, tag matching, etc. to program, we only need to master the call of these interfaces, also in pyqt The QtXml class is provided for parsing XML files. Here are a variety of xml file parsing methods. We will use the QXmlStreamReader method provided to implement XML processing. This is a stream-based parsing method, which is usually more suitable. In the case of repeated reading of data, the API interfaces provided in this method mainly have the following common ones, and of course there are many other API interfaces. You can refer to the QT official documentation.

readNext(): read the next token from the xml input stream

Name(): the name of the token, ie <name>

isStartElement(): Determines whether the currently read token is a start element, and the start element is <>

isEndElement(): Determines whether the currently read token is an end element, and the end element is

readElementText(): Read the text value corresponding to the current token, <> text value

atEnd (): determine whether it is the end of the file

With this in mind, we can call these interfaces to get the tag name and determine if it is the starting tag, and we can read the tag text information.

In the previous blog, we used weatherInfo = bytes.decode (pbyte) statement to get the weather information and print it. In fact, our weatherInfo is a character array. We can call the weatherXml= QtCore.QXmlStreamReader(weatherInfo) function directly. Read into the XML stream, so that the related operations of the XML stream can be realized through the weatherXML object. Here we write a Python class that parses the weather data obtained from Sina to solve the weather data. The code is as follows:

From PyQt5 import QtWidgets, QtCore, QtXml, QtGui, QtNetwork

From mainwindow import Ui_MainWindow

#from QtGui import QPixmap

Import res

Import TIme

Class getWeatherInfo(object):

Def __init__(self,weatherXml):

Self.weather_updateOK=0

Self.weather_city=""

Self.weather_wendu=""

self.weahter_updateTIme=""

Self.weather_suggest=""

Self.weather_fengli=""

Self.weather_fengxiang=""

Self.weather_sunrise=""

Self.weather_sunset=""

Self.forecast_weather_info_date=[]

Self.forecast_weather_info_high=[]

Self.forecast_weather_info_low=[]

Self.forecast_weather_info_dtype=[]

Self.forecast_weather_info_dfengxiang=[]

Self.forecast_weather_info_dfengli=[]

Self.forecast_weather_info_ntype=[]

Self.forecast_weather_info_nfengxiang=[]

Self.forecast_weather_info_nfengli=[]

#index

Self.weather_zhishu_name=[]

Self.weather_zhishu_vale=[]

Self.weather_zhishu_data=[]

self.prassWeatherInfo(weatherXml)

Def prassWeatherInfo(self,weatherXml):

Print("start prassWeatherInfo")

While not weatherXml.atEnd():

If weatherXml.hasError():

Print ("error: get weather data error")

Return -1

Elif weatherXml.isStartElement():

If weatherXml.name()=="city":

Self.weather_city = weatherXml.readElementText()

#print(city)

weatherXml.readNext()

Elif weatherXml.name()=="updateTIme":

self.weahter_updateTIme=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="wendu":

Self.weather_wendu=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="fengli":

Self.weather_fengli=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="shidu":

Self.weather_shidu=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="fengxiang":

Self.weather_fengxiang=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="sunrise_1":

Self.weather_sunrise=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="sunset_1":

Self.weather_sunset=weatherXml.readElementText()

weatherXml.readNext()

Elif weatherXml.name()=="environment":

Print("environment")

While not weatherXml.atEnd():

Print("test")

If weatherXml.name()=="suggest":

Self.weather_suggest=weatherXml.readElementText()

Print("suggest")

Break

Else:

weatherXml.readNext()

Elif weatherXml.name()=="forecast":

#print(weatherXml.readElementText())

Print("forecast")

weatherXml.readNext()

While not weatherXml.atEnd():

If weatherXml.isStartElement():

If weatherXml.name()=="weather":

weatherXml.readNext()

While not weatherXml.atEnd():

If weatherXml.isStartElement():

If weatherXml.name()=="date":

#print("weather info")

Date = weatherXml.readElementText()

Self.forecast_weather_info_date.append(date)

weatherXml.readNext()

#print(date)

Elif weatherXml.name()=="high":

High=weatherXml.readElementText()

Self.forecast_weather_info_high.append(high)

weatherXml.readNext()

#print(high)

Elif weatherXml.name()=="low":

Low=weatherXml.readElementText()

Self.forecast_weather_info_low.append(low)

weatherXml.readNext()

#print(low)

Elif weatherXml.name()=="day":

#print("day info")

weatherXml.readNext()

While not weatherXml.atEnd():

If weatherXml.isStartElement():

If weatherXml.name()=="type":

Type = weatherXml.readElementText()

Self.forecast_weather_info_dtype.append(type)

weatherXml.readNext()

#print("type:")

#print(type)

Elif weatherXml.name()=="fengxiang":

Ffengxiang=weatherXml.readElementText()

Self.forecast_weather_info_dfengxiang.append(ffengxiang)

weatherXml.readNext()

#print(ffengxiang)

Elif weatherXml.name()=="fengli":

Ffengli=weatherXml.readElementText()

Print("fenli")

Self.forecast_weather_info_dfengli.append(ffengli)

weatherXml.readNext()

#print(ffengli)

Else:

weatherXml.readNext()

#break

Else:

weatherXml.readNext()

Break

Elif weatherXml.name()=="night":

#print("night info:")

weatherXml.readNext()

While not weatherXml.atEnd():

If weatherXml.isStartElement():

If weatherXml.name()=="type":

Ntype=weatherXml.readElementText()

Self.forecast_weather_info_ntype.append(ntype)

weatherXml.readNext()

#print(ntype)

Elif weatherXml.name()=="fengxiang":

Nfengxiang=weatherXml.readElementText()

Self.forecast_weather_info_nfengxiang.append(nfengxiang)

weatherXml.readNext()

#print(nfengxiang)

Elif weatherXml.name()=="fengli":

Nfengli=weatherXml.readElementText()

Print("nfenli")

Self.forecast_weather_info_nfengli.append(nfengli)

weatherXml.readNext()

#print(nfengli)

Else:

weatherXml.readNext()

#break

Else:

weatherXml.readNext()

Break

Else:

weatherXml.readNext()

Else:

weatherXml.readNext()

Break

Else:

weatherXml.readNext()

Else:

weatherXml.readNext()

Break

Elif weatherXml.name()=="zhishus":

#print("zhishus:")

weatherXml.readNext()

While not weatherXml.atEnd():

If weatherXml.isStartElement():

If weatherXml.name()=="zhishu":

Print("zhishu2:")

weatherXml.readNext()

While not weatherXml.atEnd():

If weatherXml.isStartElement():

If weatherXml.name()=="name":

Self.weather_zhishu_name.append(weatherXml.readElementText())

#print("name")

#print(self.weather_zhishu_name)

weatherXml.readNext()

Elif weatherXml.name()=="value":

Self.weather_zhishu_vale.append(weatherXml.readElementText())

#print("value")

weatherXml.readNext()

Elif weatherXml.name()=="detail":

Self.weather_zhishu_data.append(weatherXml.readElementText())

#print("detail")

#print(weatherXml.readElementText())

weatherXml.readNext()

Else:

weatherXml.readNext()

Else:

weatherXml.readNext()

Break

Else:

weatherXml.readNext()

Else:

weatherXml.readNext()

Break

Else:

weatherXml.readNext()

Else:

weatherXml.readNext()

weatherXml.clear()

Self.updateOK=1

For i in range(0,5):

Print(self.weather_zhishu_name[i])

Print(self.weather_zhishu_vale[i])

Print(self.weather_zhishu_data[i])

Def updateOK(self):

Return self.updateOK

By calling this class, the weather data in the xml file can be parsed, and the corresponding weather information can be obtained by accessing the class members.

As shown in the following figure, it is the weather index data obtained by calling this class parsing:

The above is the process of parsing the entire weather xml data. I will use this class in the later blog to implement the xml data parsing in the weather demo, and complete the design and implementation of the whole weather demo on the dragonboard 410c platform.

USB 2.0 Cable

Apple-MFI-certified data cable for Apple device charging and synchronization
Apple MFI certification ensures full charging and synchronous compatibility with iPhone X / 8 Plus / 8/7 Plus / 7/6S Plus / 6S / 6 Plus / 6/5S / 5C / 5 / iPad Pro/iPad Air/iPad with Mini/Mini 2 / iPad 4 and iPod Nano 7.
Connect the iPhone, iPad or iPod to the Lightning connector via the USB connector to the charger or computer for charging or syncing.
The compact Lightning plug is compatible with almost all protective cases.
Lightning and USB plugs add a layer of protection, increasing their robustness and reducing wear; These cables have been tested for durability and can bend 95 degrees 4,000 times without interference.

Usb 2.0 Cable,Usb Wire,Usb Line,Usb Cable Extension

CHANGZHOU LESEN ELECTRONICS TECHNOLOGY CO.,LTD , https://www.china-lesencable.com

This entry was posted in on