如何从python中的odt xml文件中检索数据?

时间:2013-03-26 04:31:26

标签: python

我在python中成功撤销了odt xml文件,但我不知道如何提取xml文件数据?

有任何技术可用于提取odt xml文件数据。

这里是我提取odt xml文件的代码

#!/usr/lib/python2.7

import sys, zipfile

if len(sys.argv) < 2:
    print "input.odt & output.xml"
    sys.exit(0)

content=""
myfile = zipfile.ZipFile(sys.argv[1])
listoffiles = myfile.infolist()
for s in listoffiles:
    if s.orig_filename == 'content.xml':
        fd = open(sys.argv[2],'w')
        content = myfile.read(s.orig_filename)
        fd.write(content)
        fd.close()

1 个答案:

答案 0 :(得分:2)

Any techniques are there for pulling the odt xml file data.我假设您对解析此xml文件的内容感到好奇。如果是这种情况,我建议BeautifulSoup。 BS用于html解析,但可以更改为接受xml数据:

BS4:

from bs4 import BeautifulSoup

soup = Beautifulsoup(<xml file contents>, 'xml')

BeautifulSoup 3:

from BeautifulSoup import BeautifulStoneSoup

soup = BeautifulStoneSoup(<xml file contents>)

从这里,您可以根据文档解析数据(上面链接)。

相关问题