My code is rather simple;
f = open(r"C:\filepath\file.xml")
xml = f.read()
tree = etree.parse(xml)
When running this, I get the stack trace
tree = etree.parse(xml)
File "src/lxml/lxml.etree.pyx", line 3427, in lxml.etree.parse (src\lxml\lxml.etree.c:79801)
File "src/lxml/parser.pxi", line 1782, in lxml.etree._parseDocument (src\lxml\lxml.etree.c:115995)
File "src/lxml/parser.pxi", line 1808, in lxml.etree._parseDocumentFromURL (src\lxml\lxml.etree.c:116345)
File "src/lxml/parser.pxi", line 1712, in lxml.etree._parseDocFromFile (src\lxml\lxml.etree.c:115233)
File "src/lxml/parser.pxi", line 1115, in lxml.etree._BaseParser._parseDocFromFile (src\lxml\lxml.etree.c:109930)
File "src/lxml/parser.pxi", line 573, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:103404)
File "src/lxml/parser.pxi", line 683, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:105058)
File "src/lxml/parser.pxi", line 611, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:103924)
IOError
What's going on here?
parse()
accepts path to the XML file :
tree = etree.parse(r"C:\filepath\file.xml")
Currently your code passes actual content of the XML to parse()
, which will trigger such IOError
. You can use fromstring()
instead to create Element
object from a string containing the actual XML data. And if needed, you can then create an ElementTree
object from previously created Element
:
f = open(r"C:\filepath\file.xml")
xml = f.read()
root = etree.fromstring(xml)
tree = etree.ElementTree(root)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments