Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法确定如何从xml文件填充字典_Python_Xml - Fatal编程技术网

Python 无法确定如何从xml文件填充字典

Python 无法确定如何从xml文件填充字典,python,xml,Python,Xml,我是python的新手。我在2.7中工作,我试图解析一个XML文件来填充一个字典,并跟踪变量名的使用次数(名称会改变,因此字典会改变),它还需要跳过变量名中的数字和冒号。我知道我需要把它作为一个元素来拉,这样我就可以操纵它了,但我不知道怎么做。请帮忙。这是我回溯到的内容,以及XML代码的一部分 import xml.etree.ElementTree as ET tree = ET.parse(sample.xml) root = tree.getroot() d = {} for iec

我是python的新手。我在2.7中工作,我试图解析一个XML文件来填充一个字典,并跟踪变量名的使用次数(名称会改变,因此字典会改变),它还需要跳过变量名中的数字和冒号。我知道我需要把它作为一个元素来拉,这样我就可以操纵它了,但我不知道怎么做。请帮忙。这是我回溯到的内容,以及XML代码的一部分

import xml.etree.ElementTree as ET

tree = ET.parse(sample.xml)
root = tree.getroot()

d = {}

for iec-source in root:

    variable_code = variable.find('variable-name')

if variable_code.text == #varibale is in dictionary add count

else #add to dictionary and add count

因此,首先,您需要提取所有变量名称节点。
.find
方法将返回与指定XPATH匹配的第一个节点。
.findall
方法将返回匹配的所有节点的数组。接下来,您需要处理文本。如果知道所有变量名都有冒号,则可以在字符串上使用
.split()
。最后,您可以使用
if key in dict.keys()
检查密钥是否存在

import xml.etree.ElementTree as ET

tree = ET.parse("sample.xml")
root = tree.getroot()

dict = {}

# Loop through all nodes with tag <variable_name>
for variable_name in root.findall(".//variable_name"):

    text = variable_name.text    # Get the raw text from the xml

    variable = text.split(":")[1]    # Splits the text into an array
                                     # ["#","VARIABLE"]
                                     # keep the second element

    if variable in dict.keys():
        dict[variable] += 1    # Increment the count for that variable
    else:
        dict[variable] = 1     # Add the new variable to dict, initialize to 1
将xml.etree.ElementTree作为ET导入
tree=ET.parse(“sample.xml”)
root=tree.getroot()
dict={}
#使用标记遍历所有节点
对于root.findall(“.//变量名称”)中的变量名称:
text=variable_name.text#从xml中获取原始文本
变量=文本。拆分(“:”[1]#将文本拆分为数组
#[“#”,“变量”]
#保留第二个元素
if dict.keys()中的变量:
dict[variable]+=1#增加该变量的计数
其他:
dict[variable]=1#将新变量添加到dict,初始化为1

您能再解释一下字典中的键/值是什么吗?
.find
方法使用XPATH字符串。etree文档在构建XPATH字符串方面有一个很好的部分,但基本上我怀疑您需要
在root.findall(“.//variable name”)
中使用变量名。所以示例5:检查,检查将被添加到字典中并计数1次。我认为这是计算使用了什么变量名以及它出现多少次的最简单方法