python - 我将如何创建一个循环,为 .xml 文件中的每个孩子创建一个新元组
问题描述
所以对于我的项目,我需要获取一个 xml 文件,在 Python 中解析它,然后创建多个并行数组。我想出了如何制作一个包含所有孩子的大元组。现在我只需要想办法将它们分解成单独的元组。我认为最简单的方法是做一个 for 循环。但是,我遇到了麻烦。因此,如果我有一个 CD 列表及其信息,我想创建一个循环,将每张 CD 及其信息放入一个元组中。我该怎么办?
import xml.etree.ElementTree as ET
def get_xml_file():
tree = ET.parse('cd_catalog.xml')
return tree
def get_root(tree):
root = tree.getroot()
return root
def find_cd(tree):
cd = tree.findall('.//CD')
return cd
def build_cd_array(tree,root,cd):
for x in range(len(cd)):
array = [tuple(c.text for c in list(cd)) for cd in root.findall('CD')
print(array)
def main():
tree = get_xml_file()
root = get_root(tree)
cd = find_cd(tree)
build_cd_array(tree,root,cd)
main()
解决方案
推荐阅读
- graph - 如何使用 Intune Graph AP 获取所有托管设备的 roleScopeTagIds?
- html - 如何在VBA中将替换文本的颜色更改为html
- javascript - 从 Contenteditable div 美化 innerHTML
- javascript - 你能/如何将构造函数添加到对象中吗?
- sql - 返回连接表的不同/唯一记录
- android - 在水平 LinearLayout 中动态添加 TextView
- android - 识别和保存 Mac 地址
- php - 在我的 html 内容中输出数据库信息
- google-cloud-datastore - ndb.PolyModel 作为常规实体的祖先——坏主意?
- android - 如何设置 Flutter 应用的构建和版本号