Read XML file in Python

I am reading a jml file. The code is very simple and reads

import xml.etree.ElementTree as ET
tree = ET.parse('VOAPoints_2010_M25.jml')
root = tree.getroot()

but I get a parsing error:

ParseError: not well-formed (invalid token): line 75, column 16

the file I'm trying to read is a dataset that was used before, so I'm sure there is no problem with it.

File Can anyone help?enter image description here enter image description here

+4
source share
2 answers

Since the pound sign was a problem, you can avoid it with the symbol £. Python can even automate the replacement in an XML file by iteratively reading each line and conditionally replacing the pound symbol:

import xml.etree.ElementTree as ET

oldfile = "VOAPoints_2010_M25.jml"
newfile = "VOAPoints_2010_M25_new.jml"

with open(oldfile, 'r') as otxt:
    for rline in otxt:
        if "£" in rline:
            rline = rline.replace("£", "£")

        with open(newfile, 'a') as ntxt:
            ntxt.write(rline)

tree = ET.parse(newfile)
root = tree.getroot()
+1
source

, . ?

import xml.etree.ElementTree as ET
myParser = ET.XMLParser(encoding="utf-8")
tree = ET.parse('VOAPoints_2010_M25.jml',parser=myParser)
root = tree.getroot()
+1

Source: https://habr.com/ru/post/1619914/


All Articles