gpt4 book ai didi

python - 打开文件时lxml中的密码错误

转载 作者:行者123 更新时间:2023-12-03 09:06:23 25 4
gpt4 key购买 nike

我的代码很简单;

f = open(r"C:\filepath\file.xml")
xml = f.read()
tree = etree.parse(xml)

运行此命令时,我得到了堆栈跟踪
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

这里发生了什么?

最佳答案

parse()接受XML文件的路径:

tree = etree.parse(r"C:\filepath\file.xml")

当前,您的代码将XML的实际内容传递给 parse(),这将触发该 IOError。您可以改用 fromstring()从包含实际XML数据的字符串中创建 Element对象。然后,如果需要,您可以根据先前创建的 ElementTree创建一个 Element对象:
f = open(r"C:\filepath\file.xml")
xml = f.read()
root = etree.fromstring(xml)
tree = etree.ElementTree(root)

关于python - 打开文件时lxml中的密码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com