无法分析xml文件

2024-09-29 22:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我过去在上周运行这个文件,结果非常完美,但今天突然不行了,我没有对它做任何更改。 你能帮我吗

这是我的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"

如果你对如何解决这个问题有任何想法,请随时发表评论


Tags: thetoinidtreefortagtags
1条回答
网友
1楼 · 发布于 2024-09-29 22:34:58

(1)文件名中的部分表示您已经 AnyConv.com__CCSOPM Section 1_Master .xml文件,然后 再次尝试将具有此名称的文件复制到您的组件

还要注意,文件名在(1)之前包含一个空格,这是 奇怪的这反过来又表明您的“第一个”文件的名称 (没有(1)以空格结尾(这很奇怪)

验证文件是否确实存在。 或者将其名称更改为AnyConv.com__CCSOPM Section 1_Master.xml,即:

  • 没有尾随空间
  • 结尾没有(1)。 然后在代码中相应地更改文件名

还要注意,文件名包含一个双下划线,这也是一种奇怪的做法。 验证您的文件名中的下划线是否实际为双下划线

在计算机中保留许多同名文件(带有“数字 后缀“在它的名字里)也是一个坏习惯。 更糟糕的做法是在代码中引用这些“重复”的文件。 将文件名更改为没有此类“数字后缀”的文件名(在此 case(1))并将代码中的文件名设置为相同

还有一个提示:无论文件名是什么:

  • 文件资源管理器窗口中单击它(一次)
  • F2键,打开此文件名的版本,但到目前为止 标记的文本不包括文件扩展名(在本例中 “.xml”)
  • Ctrl-A将文本标记扩展到整个名称 (带扩展名)
  • Ctrl-C将文件名复制到剪贴板
  • Esc关闭文件名的版本
  • 打开代码编辑器,将光标放在文件名所在的位置,然后 标记整个文件名
  • Ctrl-V将其替换为剪贴板内容

现在,在代码中,您将获得实际的文件名

还要检查包含此文件的目录是否在列表中 试图打开文件时Python扫描的目录的数目

相关问题 更多 >

    热门问题