Python Parse XML correct application code example [Copy link]
|
If you want to usePythonHow can I complete XML-related operations in programming languages? Here we will learn about the method of parsing XML in Python, and I believe it can help you fully master the application technology in this area. Python parsing XML code example:<Text> 1. <font size = "86" spacing = "17"/> Rate improvement without the Tang Yuan's method, which has been written in the western regions for seven years 2. </text> <Text> 3. Rate improvement without using the Tang Yuan's method, which has been written in the western regions for more than seven years </Text> 4. Import XML. etree. elementtree Tree = xml. etree. elementtree. elementtree (file = "E: \ Python \ 1.xml ") 5. ELEM = tree. getroot () File = open ("E: \ a.txt", 'w ') 6. For a in ELEM. getiterator ("text "): # Print Len (A. _ children) 7. If Len (A. _ children)> 0: For node in: 8. If node. Tail <> none: Node. Tail. encode ('utf-8 ') 9. file. Write (node. Tail. encode ('utf-8 ')) # Print XML. etree. elementtree. iselement (node) 10. Else: If a. Text <> none: 11. file. Write (A. Text. encode ('utf-8 ')) The above isPythonIntroduction to XML parsing methods. |
|