Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import xml.etree.ElementTree as ET
- tree = ET.parse('fields.asc.aux.xml')
- root = tree.getroot()
- print root.tag
- level = 0
- for child in root:
- if level<1:
- level = 1
- for child2 in child:
- if level < 2:
- level = 2
- for child3 in child2:
- if level < 3:
- level = 3
- for child4 in child3:
- if level < 4:
- level = 4
- for child5 in child4:
- if level < 5:
- level = 5
- hlavicka = "root;"
- for i in range(1,level+1):
- hlavicka= hlavicka + "level "+str(i) +";"
- print hlavicka
- csv = open("fields.csv","w")
- csv.write(hlavicka+"\n")
- csv.write(root.tag)
- if len(root.attrib) > 0:
- csv.write(str(root.attrib))
- if root.text != None:
- roottext = ''.join(root.text.split())
- print roottext
- if len(roottext) > 0:
- rtext = roottext.replace(";",",")
- csv.write(rtext)
- for i in range(0,level+1):
- csv.write(";")
- csv.write("\n")
- for child in root:
- print child.tag, child.attrib, child.text
- csv.write(";"+child.tag)
- if len(child.attrib) > 0:
- csv.write(": "+str(child.attrib))
- if child.text != None:
- childtext = ''.join(child.text.split())
- if len(childtext) > 0:
- text = childtext.replace(";", ",")
- csv.write(": "+ text)
- for i in range(1, level + 1):
- csv.write(";")
- csv.write("\n")
- for child2 in child:
- print child2.tag, child2.attrib, child2.text
- csv.write(";;" + child2.tag)
- if len(child2.attrib) > 0:
- csv.write(": " + str(child2.attrib))
- if child2.text != None:
- child2text = ''.join(child2.text.split())
- if len(child2text) > 0:
- text2 = child2text.replace(";", ",")
- csv.write(": " + text2)
- for i in range(2, level + 1):
- csv.write(";")
- csv.write("\n")
- for child3 in child2:
- print child3.attrib, child3.tag, child3.text
- csv.write(";;;" + child3.tag)
- if len(child3.attrib) > 0:
- csv.write(": " + str(child3.attrib))
- if child3.text != None:
- child3text = ''.join(child3.text.split())
- if len(child3text) > 0:
- text3 = child3text.replace(";", ",")
- csv.write(": " + text3)
- for i in range(3, level + 1):
- csv.write(";")
- csv.write("\n")
- for child4 in child3:
- print child4.attrib, child4.tag, child4.text
- csv.write(";;;;" + child4.tag)
- if len(child4.attrib) > 0:
- csv.write(": " + str(child4.attrib))
- if child4.text != None:
- child4text = ''.join(child4.text.split())
- if len(child4text) > 0:
- text4 = child4text.replace(";", ",")
- csv.write(": " + text4)
- for i in range(4, level + 1):
- csv.write(";")
- csv.write("\n")
- for child5 in child4:
- csv.write(";;;;;" + child5.tag)
- if len(child5.attrib) > 0:
- csv.write(": " + str(child5.attrib))
- if child5.text != None:
- child5text = ''.join(child5.text.split())
- if len(child5text) > 0:
- text5 = child5text.replace(";", ",")
- csv.write(": " + text5)
- for i in range(5, level + 1):
- csv.write(";")
- csv.write("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement