0

hi all,

how to parse an xml attributes using python. I want to read or extract msg values from xml using python.

         <msg timestamp="20160817 12:46:42.520" level="INFO">Average : 0.14 %
CPU (%)
10 Aug 07:00
2
12 Aug 07:00
maximum 5.17
average 0.13</msg>

Edited by pritaeas: Moved to programming.

2
Contributors
1
Reply
19
Views
1 Year
Discussion Span
Last Post by snippsat
0

Your pasting of xml is a mess:)
The best is to use Beautiful Soup/lxml.
E.g

from bs4 import BeautifulSoup

xml ='''\
<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Hello</to>
  <from>wold</from>
  <msg timestamp="20160817 12:46:42.520" level="INFO">Info on CPU</msg>
</note>'''

#soup = BeautifulSoup(open("your.xml"), 'html.parser') #Read from a file
soup = BeautifulSoup(xml, 'html.parser')
msg = soup.find('msg')
print(msg.text) #--> Info on CPU
print(msg.get('timestamp')) #--> 20160817 12:46:42.520

Now i use html.parser which in the standard library.
If lxml is installed,use always this parser soup = BeautifulSoup(xml, 'lxml-xml') or BeautifulSoup(xml, 'lxml')
Not that matter for you,because you have not use old BS.
New is BS you have to specify as parser as i have shown here.

Now you have posted this under Hardware and Software Cloud-based Applications
Which is not what this is about.

Edited by snippsat

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.