有很多关于用elementtree解析xml的文章。我已经阅读了一大堆的文档,但是我不能想出一个适合我的解决方案。我试图补充信息,这是由另一个应用程序在一个nfo文件中创建的,但我需要保留文件中的约定。你知道吗
下面是一个文件布局的示例
<title>
<name>Test Name</name>
<alt name />
<file local="C:\file\file1.doc" type="word">http://filestore/file1.doc</file>
<file local="" type="excel">http://filestore/file2.xls</file>
<file local="C:\file\file3.xls" type="excel" />
<file local="" type="ppt" />
</title>
注意:元件未正确关闭,例如。。。你知道吗
<alt name /> should be <alt name></alt name>
这就是我要做的。。。你知道吗
import xml.etree.ElementTree as ET
tree = ET.parse('file.nfo')
root = tree.getroot()
我得到的错误是。。。你知道吗
xml.etree.ElementTree.ParseError: not well-formed (invalid token):
我试过。。。你知道吗
myparser = ET.XMLParser(encoding='UTF-8')
tree = ET.parse('file.nfo', myparser)
我也尝试过,xmlparser,用编解码器打开,但我很确定它的格式。我猜眼前的问题是无法逃脱的>;但我怀疑ET需要打开/关闭?你知道吗
我确信我可以打开这个文件并用regex检查它,但是我希望使用ElementTree。你知道吗
最终目标是把nfo中的细节作为一个字典,看起来像。。。你知道吗
dict = {'title': [{'name': 'Test Name',
'alt name': '',
'file': [{'local': 'C:\file\file1.doc', 'type': 'word', 'url': 'http://filestore/file1.doc'},
{'local': '', 'type': 'excel', 'url': 'http://filestore/file2.xls'},
{'local': 'C:\file\file3.xls', 'type': 'excel', 'url': ''},
{'local': '', 'type': 'ppt', 'url': ''}]
}]}
我确信有更好的(更具python风格的)方法可以做到这一点,但我对python还很陌生。你知道吗
任何帮助都将不胜感激
编辑:如果可能的话,我也尽量避免使用第三方库
所以我最终创建了一个客户解析器,虽然不是很理想,但它是有效的。有人向我建议lxml和html.parser语法分析器可能会更好地解析格式错误的xml,但我只是同意这个。你知道吗
我仍然对任何反馈非常感兴趣,无论是关于这个还是使用任何其他方法。你知道吗
注意:我把最上面的字典从原来的帖子里删掉了
相关问题 更多 >
编程相关推荐