user12762382
user12762382

Reputation:

Python script to extract strings and ints between xml tags

I need help extracting Things between XML tags. For example:

 <TimeStamp>DT#2019-08-27-08:20:51</TimeStamp>' ,  '<UserName>admin</UserName>' ,  '<Generator>SG-22-6500</Generator>' ,  '<GenFreq>20</GenFreq>' ,  '<MaxForce>10000</MaxForce>', '<MaxPower>6500</MaxPower>, 

And now i want to have the values Dates and strings between the XML tags. The Output i wish for would be:

2019-08-27-08:20:51,admin,SG-22-6500,20,10000

and if there isnt anything between the tags, just print n.A for example.

i already tried the libary XML.etree, but i think my Problem is, my values do not come from a XML file…

Best regards!

Upvotes: 1

Views: 86

Answers (3)

hurlenko
hurlenko

Reputation: 1425

You can use builtin xml parser

import xml.etree.ElementTree as ET

x = [
    "<TimeStamp>DT#2019-08-27-08:20:51</TimeStamp>",
    "<UserName>admin</UserName>",
    "<Generator>SG-22-6500</Generator>",
    "<GenFreq>20</GenFreq>",
    "<MaxForce>10000</MaxForce>",
    "<MaxPower>6500</MaxPower>",
]

for i in x:
    tree = ET.ElementTree(ET.fromstring(i)).getroot()
    print(tree.text)

Prints

DT#2019-08-27-08:20:51
admin
SG-22-6500
20
10000
6500

Upvotes: 0

David Kalamarides
David Kalamarides

Reputation: 56

If you have a large XML, I would recommend Beautiful Soup

from bs4 import BeautifulSoup as bsoup
[bsoup(x).text for x in xml_list]

Alternatively, if you just need to pull out the text from a single Tag instead of a large file, try using a regular expression.

import re
[re.sub('<.*?>','',x) for x in xml_list]

Upvotes: 1

gbeaven
gbeaven

Reputation: 1800

Use split:

xml_var = '<TimeStamp>DT#2019-08-27-08:20:51</TimeStamp>'
xml_inner = xml_var.split('<TimeStamp>')[1].split('</TimeStamp>')[0]
print(xml_inner)

Will return whatever's between the TimeStamp tag. No need to over-engineer anything.

Upvotes: 0

Related Questions