关于做芯片类招聘的网站,网站设计psd模板,建网站的系统,有没有免费的企业邮箱我通常使用标准库中的ElementTree模块解析XML。它没有给你一个字典#xff0c;你得到了一个更有用的DOM结构#xff0c;它允许你为孩子们遍历每个元素。from xml.etree import ElementTree as ETxml ET.parse(root_element xml.getroot()for child in root_element:.…我通常使用标准库中的ElementTree模块解析XML。它没有给你一个字典你得到了一个更有用的DOM结构它允许你为孩子们遍历每个元素。from xml.etree import ElementTree as ETxml ET.parse(root_element xml.getroot()for child in root_element:...如果需要将其解析为字典而不是从DOM树中获取所需的信息则从根节点构建字典的递归函数如下def xml_dict(node, path, dic None):if dic None:dic {}name_prefix path (. if path else ) node.tagnumbers set()for similar_name in dic.keys():if similar_name.startswith(name_prefix):numbers.add(int (similar_name[len(name_prefix):].split(.)[0] ) )if not numbers:numbers.add(0)index max(numbers) 1name name_prefix str(index)dic[name] node.text ....join(childnode.tailif childnode.tail is not None else for childnode in node)for childnode in node:xml_dict(childnode, name, dic)return dic对于上面列出的XML将生成此字典{A1: \n \n ...\n,A1.B1: \n \n ...\n ,A1.B1.C1: blah,A1.B1.C2: blah,A1.B2: \n \n ...\n ,A1.B2.C1: blah,A1.B2.C2: blah}(我发现DOM表单更有用)