【发布时间】:2021-10-06 04:55:44
【问题描述】:
上周我曾经运行过这个文件,结果很完美,但今天突然不行了,我没有对其进行任何更改。 你能帮我吗?
这是我的python代码:
from lxml import etree
import numpy as np
#Parsing the xml file and creating lists
tree = etree.parse("AnyConv.com__CCSOPM Section 1_Master (1).xml")
root = tree.getroot()
Lista = []
tags = []
#Get the unique tags values
for element in root.iter():
Lista.append(element.tag)
tags = np.unique(Lista)
#Show the unique tag[attributes] pairs
for tag in tags:
print(tag,root.xpath(f'//{tag}')[0].attrib.keys())
#Changes the tag name to the comply365's tag's name
for p in tree.findall(".//sect1"):
p.tag = ("section")
for p in tree.findall(".//sect1"):
p.tag = ("section")
for p in tree.findall(".//informaltable"):
p.tag = ("table")
#Modify the tag's attributes to its desired form
for cy in root.xpath('//section'):
cy.attrib['xmlns']='http://www.w3.org/2001/XMLSchema-instance'
cy.attrib['id']='123'
cy.attrib['type']='policy'
cy.attrib['xsi']='urn:fontoxml:cpa.xsd:1.0'
for t in root.xpath('//title'):
t.attrib['id']='123456789'
for p in root.xpath('//para'):
p.attrib['id']='987654321'
for p in root.xpath('//table'):
p.attrib['id']='11111'
for ct in root.xpath('//concept'):
ct.attrib.pop("id", None)
#Print the new xml to make sure it worked:
#print(etree.tostring(root).decode())
tree.write("Resultado de tags XML-COMPLY365.xml")
这会导致:
OSError: Error reading file 'AnyConv.com__CCSOPM Section 1_Master (1).xml': failed to load external entity "AnyConv.com__CCSOPM Section 1_Master (1).xml"
如果您有任何解决方法的想法,请随时在下面发表评论。
【问题讨论】:
-
该错误表明您的 XML 文件存在问题。由于您尚未与我们分享这一点,因此我们无能为力。
-
不管是什么文件,只要是XML文件就不会解析。
-
您确定该文件存在于您认为的位置吗?您正在尝试读取当前目录中名为“AnyConv.com__CCSOPM Section 1_Master (1).xml”的文件。也许您需要提供明确的路径?如果您向我们展示您已经尝试过的诊断步骤,我们可能会提供更好的帮助。例如,创建一个示例 XML 文件,运行一个交互式 Python 会话并尝试调用
etree.parse,然后向我们展示完整的脚本。 -
谢谢!原来我在不同的文件夹中复制了同名的文件。我疯狂地到处寻找错误,结果就是这样。对于这么愚蠢的问题,我深表歉意,感谢大家的宝贵时间!
标签: python xml xml-parsing lxml