Feb-22-2018, 04:30 PM
So you want all children of the
The easiest way to get that sort of info, is to parse it as xml, and just jump to the part you care about. They're small snippets, so using a DOM parser is easier than using SAX.
Using the xml.etree.ElementTree in the standard lib, that means we could do something like this:
data
element, except the sensor
element, but only if the sensor
element's value is 0. That's the xml path of /msg/hist/data
, and only the first match.The easiest way to get that sort of info, is to parse it as xml, and just jump to the part you care about. They're small snippets, so using a DOM parser is easier than using SAX.
Using the xml.etree.ElementTree in the standard lib, that means we could do something like this:
messages = [ '''<msg><src>CC128v1.48</src><dsb>01339</dsb><time>15:17:50</time><hist><dsw>01341</dsw><type>1</type><units>kwhr</units><data><sensor>0</sensor><d058>1.710</d058><d057>1.460</d057><d056>1.664</d056><d055>1.585</d055></data><data><sensor>1</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>2</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>3</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>4</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>5</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>6</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>7</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>8</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data><data><sensor>9</sensor><d058>0.000</d058><d057>0.000</d057><d056>0.000</d056><d055>0.000</d055></data></hist></msg>''', '''<msg><src>CC128v1.48</src><dsb>01339</dsb><time>15:21:20</time><hist><dsw>01341</dsw><type>1</type><units>kwhr</units><data><sensor>0</sensor><m020>279.750</m020><m019>304.500</m019><m018>326.000</m018><m017>308.750</m017></data><data><sensor>1</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>2</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>3</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>4</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>5</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>6</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>7</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>8</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data><data><sensor>9</sensor><m020>0.000</m020><m019>0.000</m019><m018>0.000</m018><m017>0.000</m017></data></hist></msg>''' ] import xml.etree.ElementTree as ET for message in messages: root = ET.fromstring(message) data_elems = root.findall("./hist/data") data = data_elems[0] children = data.getchildren() # first element is "sensor", which we don't want for elem in children[1:]: name = elem.tag value = elem.text print("{0} => {1}".format(name, value))Which gives:
Output:>python spam.py
d058 => 1.710
d057 => 1.460
d056 => 1.664
d055 => 1.585
m020 => 279.750
m019 => 304.500
m018 => 326.000
m017 => 308.750
You could also use BeautifulSoup for a nicer/easier way to get the data, but that'd be a package you'd need to install.