2013-03-26 190 views
1

我成功地在python中删除odt xml文件,但我不知道如何拉取xml文件数据?如何从python中检索odt 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() 

回答

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>) 

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