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 文件,return 将属性放入列表,然后将这些列表输入 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>
我能够通过 return 列表解析 id="bk101" 的第一本书节点或 id="bk103" 的最后一本书节点的正确属性正确的属性。但是,当我需要 return 多个列表时,我只 return 每个文件一个列表(每个书节点和信息节点一个,所以在这种情况下总共有 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)
所以我知道我的问题是每个函数只 returning 一个列表,因为我在函数末尾 returning 列表,然后在末尾调用函数我的剧本。我只是找不到实现我想要的结果的正确方法。如果我不在脚本末尾 运行 我的函数,那么我如何 return 我要查找的多个列表?
这一行是你的问题:
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']]
您的另一个 method/loop 也有同样的问题 - 您将新列表分配给变量,而不是将新列表插入现有列表。
我正在使用 xml.etree.ElementTree 模块解析 XML 文件,return 将属性放入列表,然后将这些列表输入 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>
我能够通过 return 列表解析 id="bk101" 的第一本书节点或 id="bk103" 的最后一本书节点的正确属性正确的属性。但是,当我需要 return 多个列表时,我只 return 每个文件一个列表(每个书节点和信息节点一个,所以在这种情况下总共有 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)
所以我知道我的问题是每个函数只 returning 一个列表,因为我在函数末尾 returning 列表,然后在末尾调用函数我的剧本。我只是找不到实现我想要的结果的正确方法。如果我不在脚本末尾 运行 我的函数,那么我如何 return 我要查找的多个列表?
这一行是你的问题:
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']]
您的另一个 method/loop 也有同样的问题 - 您将新列表分配给变量,而不是将新列表插入现有列表。