繁体   English   中英

Python - 如何使用xml.etree.ElementTree为我正在迭代的每个xml节点返回一个列表?

[英]Python - How can I return a list for each xml node I am iterating through using xml.etree.ElementTree?

我正在使用xml.etree.ElementTree模块来解析XML文件,将属性返回到列表中,然后在MySQL数据库中输入这些列表(这个最后一步我并不担心,所以这里不需要覆盖它) 。 很简单,我目前能够这样做,但一次只能用于一个子节点。 目标是使用多个子节点,无论有多少个子节点。 这是一个示例文件:

<?xml version="1.0"?>
    <catalog>
       <book id="bk101" type="hardcover">
          <info author="Gambardella, Matthew" title="XML Developer's Guide" genre="Computer" price="44.95" publish_date="2000-10-01" description="An in-depth look at creating applications 
          with XML." />
       </book>
       <book id="bk102" type="softcover">
          <info author="Ralls, Kim" title="Midnight Rain" genre="Fantasy" price="5.95" publish_date="2000-10-01" description="A former architect battles corporate zombies, 
          an evil sorceress, and her own childhood to become queen 
          of the world." />
       </book>
       <book id="bk101" type="softcover">
          <info author="Corets, Eva" title="Maeve Ascendant" genre="Fantasy" price="5.95" publish_date="2000-11-17" description="After the collapse of a nanotechnology 
          society in England, the young survivors lay the 
          foundation for a new society." />
       </book>
    </catalog>

我能够通过返回具有正确属性的列表来解析第一个书籍节点的正确属性,其中id =“bk101”或最后一个书籍节点,其中id =“bk103”。 但是,当我需要返回多个列表时,每个文件只返回一个列表(每个书节点和信息节点一个,因此在这种情况下总共有6个列表)。

这是我的代码:

import xml.etree.ElementTree

book_attribute = ['id', 'type']
info_attribute = ['author', 'title', 'genre', 'price', 'publish_date', 'description']


class ApplicationClass(object):  # define the only class in this file
    def __init__(self):
        self.ET = xml.etree.ElementTree.parse('file.xml').getroot()
        self.bookNodes = self.ET.findall('book')
        self.book_values_list = []
        self.info_values_list = []

    def get_book(self):
        for bookNode in self.bookNodes:
            self.book_values_list = [bookNode.get(i) for i in book_attribute]
        return self.book_values_list

    def get_info(self):
        for bookNode in self.bookNodes:
            for infoNode in bookNode.findall('info'):
                self.info_values_list = [infoNode.get(i) for i in info_attribute]
        return self.info_values_list

a = ApplicationClass()
a.get_book()
print(a.book_values_list)
a.get_info()
print(a.info_values_list)

所以我知道我的问题是我每个函数只返回一个列表,因为我在函数末尾返回列表,然后在脚本结束时调用函数。 我找不到达到预期结果的正确方法。 如果我不在脚本结束时运行我的函数,那么如何返回我正在寻找的多个列表?

这行是你的问题:

self.book_values_list = [bookNode.get(i) for i in book_attribute]

该行将使用新列表替换现有列表。 但是你在循环中有这条线,这意味着在每次通过循环时,你会丢失先前处理的内容。

我想你想要这个:

self.book_values_list.append([bookNode.get(i) for i in book_attribute])

使用.append()而不是=会使你的变量插入更多的东西。 最终你会得到一个列表列表,如下所示:

[['bk101', 'hardcover'], ['bk102', 'softcover'], ['bk101', 'softcover']]

您的其他方法/循环也存在相同的问题 - 您为变量分配了一个新列表,而不是将新列表插入现有列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM