Python expert needed :-)

Python and python framework

Moderator: leecollings

Post Reply
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Python expert needed :-)

Post by D'rMorris »

hi guys,

For a project, I'm working on processing an XML file in python. An example of the file:

Code: Select all

<?xml version="1.0"?>
<log>
	<header>
		<name>Alarm Log</name>
		<serial_no>123456789</serial_no>
		<sw_ver>W</sw_ver>
		<file_ver>1.0</file_ver>
	</header>
	<record>
		<data name="date">03-Sep-2014</data>
		<data name="time">01:15:48pm</data>
		<data name="event" id="123">Event text</data>
		<data name="urgency" id="456">Urgency text</data>
		<data name="alarm" id="15">Alarm text</data>
		<data name="analysis">Analysis text</data>
	</record>
	<record>
		<data name="date">01-Sep-2014</data>
		<data name="time">12:45:14pm</data>
		<data name="event" id="789">Detection</data>
		<data name="urgency" id="012">HIGH</data>
		<data name="alarm" id="11">ALARM TEXT</data>
		<data name="analysis">Analysis text</data>
	</record>
</log>
What I'm trying to do is to convert the different XML values into an URL. So far, I have the following code:

Code: Select all

import xmltodict
import ordereddict

with open('C:\\alarms_log.xml') as fd:
    doc = xmltodict.parse(fd.read())

serial = doc['log']['header']['serial_no']
device = doc['log']['header']['name']

print serial
print device
Output:

Code: Select all

123456789
Alarm Log
So, I can read the header without issues. What I need to do next, is to read the different data elements under the "record" tag. The record tag can appear multiple times within one XML file.

In the example above, I used data = doc['log']['record'][0] and data = doc['log']['record'][1] to see what the output is:

Code: Select all

data = doc['log']['record'][0]
data1 = doc['log']['record'][1]

print data
print data1
This is:

Code: Select all

OrderedDict([(u'data', [OrderedDict([(u'@name', u'date'), ('#text', u'03-Sep-2014')]), OrderedDict([(u'@name', u'time'), ('#text', u'01:15:48pm')]), OrderedDict([(u'@name', u'event'), (u'@id', u'123'), ('#text', u'Event text')]), OrderedDict([(u'@name', u'urgency'), (u'@id', u'456'), ('#text', u'Urgency text')]), OrderedDict([(u'@name', u'alarm'), (u'@id', u'15'), ('#text', u'Alarm text')]), OrderedDict([(u'@name', u'analysis'), ('#text', u'Analysis text')])])])

and:

OrderedDict([(u'data', [OrderedDict([(u'@name', u'date'), ('#text', u'01-Sep-2014')]), OrderedDict([(u'@name', u'time'), ('#text', u'12:45:14pm')]), OrderedDict([(u'@name', u'event'), (u'@id', u'789'), ('#text', u'Detection')]), OrderedDict([(u'@name', u'urgency'), (u'@id', u'012'), ('#text', u'HIGH')]), OrderedDict([(u'@name', u'alarm'), (u'@id', u'11'), ('#text', u'ALARM TEXT')]), OrderedDict([(u'@name', u'analysis'), ('#text', u'Analysis text')])])])
My question is: how can I now process this OrderedDict in Python and put the 6 data elements into variables (so 6 variables per record) like I did with the serial and device? Afterwards I'll loop through the variables for each record and process it, after the next record is processed.

Thanks!
ubee
Posts: 66
Joined: Tuesday 10 February 2015 20:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Sweden
Contact:

Re: Python expert needed :-)

Post by ubee »

I belive you will get better response posting you question on stackoverflow.com. You will find many Python experts there!
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Python expert needed :-)

Post by gerardvs »

Something like this?

Code: Select all

import xmltodict

with open('alarms_log.xml') as fd:
    doc = xmltodict.parse(fd.read())

serial = doc['log']['header']['serial_no']
device = doc['log']['header']['name']

print (serial)
print (device)
print ('=============================')


for rec_no in range(len(doc['log']['record'])):
    date = doc['log']['record'][rec_no]['data'][0]['#text']
    time = doc['log']['record'][rec_no]['data'][1]['#text']
    eventid = doc['log']['record'][rec_no]['data'][2]['@id']
    eventtxt = doc['log']['record'][rec_no]['data'][2]['#text']
    urgencyid = doc['log']['record'][rec_no]['data'][3]['@id']
    urgencytxt = doc['log']['record'][rec_no]['data'][3]['#text']
    alarmid = doc['log']['record'][rec_no]['data'][4]['@id']
    alarmtxt = doc['log']['record'][rec_no]['data'][4]['#text']
    analysis = doc['log']['record'][rec_no]['data'][5]['#text']

    print ("date:       " + date)
    print ("time:       " + time)
    print ("eventid:    " + eventid)
    print ("eventtxt:   " + eventtxt)
    print ("urgencyid:  " + urgencyid)
    print ("urgencytxt: " + urgencytxt)
    print ("alarmid:    " + alarmid)
    print ("alarmtxt:   " + alarmtxt)
    print ("analysis:   " + analysis)
    print ('=============================')
D'rMorris
Posts: 138
Joined: Thursday 01 May 2014 9:01
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Netherlands - Sittard
Contact:

Re: Python expert needed :-)

Post by D'rMorris »

gerardvs wrote:Something like this?
EXACTLY like that! Thank you very much for your help :D
gerardvs
Posts: 81
Joined: Sunday 04 January 2015 0:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest-1
Location: /dev/null
Contact:

Re: Python expert needed :-)

Post by gerardvs »

Sure, you have to make it, in good English "hufterproof". On the other hand, ordereddict should give a predictable result.
--Gerard
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest