python loop for a xml

0

I have a file where with a regular expression I get 3 data that can be repeated up to 30 times

matches = tools.findall(pattern, html, re.MULTILINE)
for match in matches:

    pattern = 'umber\":(.*?),\"g'
    dato1 = tools.findall(pattern, match, re.MULTILINE)[0]

    pattern = '\],\"name\"\:\"(.*?)\",\"ov'
    dato2 = tools.findall(pattern, match, re.MULTILINE)[0]

    pattern = '\"overview\"\:\"(.*?)\",\"id'
    dato3 = tools.findall(pattern, match, re.MULTILINE)[0]

my intension is with these data generate an xml in the following way:

<root>
    <item>
       <nombre>dato1</nombre>
       <direccion>dato2</direccion>
       <telefono>dato3</telefono>
    </item>
    <item>
       <nombre>dato1</nombre>
       <direccion>dato2</direccion>
       <telefono>dato3</telefono>
    </item>
    <item>
       <nombre>dato1</nombre>
       <direccion>dato2</direccion>
       <telefono>dato3</telefono>
    </item>
<root>

the items I want to repeat as many data are collected from the for loop

    
asked by Jesus Obregon 21.06.2017 в 19:43
source

3 answers

0

A simple way if I understood what you were asking is to manually compose the xml in the following way

matches = tools.findall(pattern, html, re.MULTILINE)
xml_text = "<root>"
for match in matches:

    pattern = 'umber\":(.*?),\"g'
    dato1 = tools.findall(pattern, match, re.MULTILINE)[0]

    pattern = '\],\"name\"\:\"(.*?)\",\"ov'
    dato2 = tools.findall(pattern, match, re.MULTILINE)[0]

    pattern = '\"overview\"\:\"(.*?)\",\"id'
    dato3 = tools.findall(pattern, match, re.MULTILINE)[0]

    xmltext = xmltext + "  <item>\n     <nombre>{0}</nombre>\n     <direccion>{1}</direccion>\n     <telefono>{2}</telefono>\n  </item>\n".format(dato1, dato2, dato3)

xml_text = xml_text + "</root>"
print(xml_text)
    
answered by 21.06.2017 / 19:50
source
0

Use the xml library, for this specific process use xml.etree.ElementTree

generate your main elemnt with:

root = ElementTree.Element("root")

then add the sub-elements with

ElementTree.SubElement(root, 'nombre').text='dato1'

finally you generate the xml with

ElementTree.dump(root)

the complete example would look like this:

from xml.etree import ElementTree

root = ElementTree.Element('root')

matches = tools.findall(pattern, html, re.MULTILINE)

for match in matches:      


    pattern = 'umber\":(.*?),\"g'
    dato1 = tools.findall(pattern, match, re.MULTILINE)[0]

    pattern = '\],\"name\"\:\"(.*?)\",\"ov'
    dato2 = tools.findall(pattern, match, re.MULTILINE)[0]

    pattern = '\"overview\"\:\"(.*?)\",\"id'
    dato3 = tools.findall(pattern, match, re.MULTILINE)[0]

    item=ElementTree.SubElement(root, 'item')
    ElementTree.SubElement(item, 'nombre').text='dato1'
    ElementTree.SubElement(item, 'direccion').text='dato2'
    ElementTree.SubElement(item, 'telefono').text='dato3'

ElementTree.dump(root)
    
answered by 21.06.2017 в 20:20
0

code that did the function.

for match in matches:
pattern = 'umber\":(.*?),\"g'
dato1 = tools.findall(pattern, match, re.MULTILINE)[0]

pattern = '\],\"name\"\:\"(.*?)\",\"ov'
dato2 = tools.findall(pattern, match, re.MULTILINE)[0]

pattern = '\"overview\"\:\"(.*?)\",\"id'
dato3 = tools.findall(pattern, match, re.MULTILINE)[0]

xmltext = "  <item>\n     <nombre>{0}</nombre>\n     <direccion>{1}</direccion>\n     <telefono>{2}</telefono>\n  </item>\n".format(
    dato1, dato2, dato3)

fichero = open ( 'filename.xml', 'a' )
fichero.write(xmltext)
    
answered by 21.06.2017 в 20:28