XML文件的Python比较

2024-10-01 17:27:16 发布

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

我有两个大的XML文件(c.100MB),其中包含许多项。我想把它们之间的差别计算出来。在

每个项目都有一个ID,我需要检查它是否在两个文件中。如果是这样的话,我需要比较该项的各个值,以确保它是同一项。在

SAX解析器是解决这个问题的最佳方法吗?它是如何使用的?我使用了element tree和findall来处理较小的文件,但现在我不能处理大文件。在

srcTree = ElementTree()
srcTree.parse(srcFile)

# finds all the items in both files
srcComponents = (srcTree.find('source')).find('items')
srcItems = srcComponents.findall('item')
dstComponents = (dstTree.find('source')).find('items')
dstItems = dstComponents.findall('item')

# parses the source file to find the values of various fields of each
# item and adds the information to the source set
for item in srcItems:
  srcId = item.get('id')
  srcList = [srcId]
  details = item.find('values')
  srcVariables = details.findall('value')
  for var in srcVariables:
    srcList.append((var.get('name'),var.text))
srcList = tuple(srcList)
srcSet.add(srcList)

Tags: 文件thetoinsourcevaritemsfind
1条回答
网友
1楼 · 发布于 2024-10-01 17:27:16

您可以使用elementtree作为pull解析器(比如sax)http://effbot.org/zone/element-pull.htm 在elementreehttp://effbot.org/zone/element-iterparse.htm中还有一个iterparse函数 这两种方法都允许您处理大文件,而无需将所有内容加载到内存中。在

但是sax可以工作(我已经用它处理了远远大于100MB的数据),但是现在我将使用elementtree来完成这项工作。在

还可以看看使用lxml(etree兼容)http://lxml.de/tutorial.html#event-driven-parsing进行的增量/基于事件的解析

这里有一篇关于在文件中使用iterparse的好文章>;1GBhttp://www.ibm.com/developerworks/xml/library/x-hiperfparse/

相关问题 更多 >

    热门问题