本文介绍了使用 Python 从 XML 文件中提取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的链接为我们提供了配方列表中的成分列表.我想提取成分的名称并使用 python 将其保存到另一个文件中.http://stream.massey.ac.nz/file.php/6087/Eva_Material/Tutorials/recipebook.xml

The link below gives us the list of ingredients in recipelist. I would like to extract the names of the ingredient and save it to another file using python.http://stream.massey.ac.nz/file.php/6087/Eva_Material/Tutorials/recipebook.xml

到目前为止,我已经尝试使用以下代码,但它给了我完整的配方,而不是成分的名称:

So far I have tried using the following code, but it gives me the complete recipe not the names of the ingredients:

from xml.sax.handler import ContentHandler
import xml.sax
import sys
def recipeBook():
    path = "C:\Users\user\Desktop"
    basename = "recipebook.xml"
    filename = path+"\\"+basename
    file=open(filename,"rt")
    # find contents
    contents = file.read()

    class textHandler(ContentHandler):
      def characters(self, ch):
      sys.stdout.write(ch.encode("Latin-1"))
    parser = xml.sax.make_parser()
    handler = textHandler( )
    parser.setContentHandler(handler)
    parser.parse("C:\Users\user\Desktop\\recipebook.xml")



  file.close()

如何提取每种成分的名称并将它们保存到另一个文件中?

How do I extract the name of each ingredient and save them to another file?

推荐答案

@Neha

我猜您现在已经解决了您的请求,这是我使用 http 上的教程整理的一小部分://lxml.de/tutorial.html.XML 文件保存在 'rough_data.xml'

I guess you have solved your request by now, here is a little piece I put together using the tutorial at http://lxml.de/tutorial.html. The XML file is saved in 'rough_data.xml'

import xml.etree.cElementTree as etree

xmlDoc = open('rough_data.xml', 'r')
xmlDocData = xmlDoc.read()
xmlDocTree = etree.XML(xmlDocData)

for ingredient in xmlDocTree.iter('ingredient'):
    print ingredient[0].text

致所有有经验的 Python 程序员阅读本文,请改进这段新手"代码.

To all experienced Python programmers reading this, kindly improve this "newbie" code.

注意:lxml 包看起来很不错,绝对值得使用.谢谢

Note: The lxml package looks very good, it definitely worth using.Thanks

这篇关于使用 Python 从 XML 文件中提取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:08