如何在python中对所有文件xml的循环结果求和



我是编程新手,遇到了一个问题。我有4个这样的文件

<object>
<pose>Damage</pose>
<pose>Dent</pose>
<pose>Damage</pose>
<pose>Dent</pose>
<pose>Damage</pose>
</object>

我想在所有文件中计算损坏和凹痕的数量。我已经这样做了,但没有得到我需要的结果。

import os
import xml.etree.ElementTree as ET
from collections import defaultdict

# files are in a sub folder where this script is being ran
path = r"D:Non_Documentsxml"
for filename in os.listdir(path):
# Only get xml files
if not filename.endswith('.xml'): continue
# I haven't been able to get it to work by just saying 'if filename.endswith('.xml')' only if not..
fullname = os.path.join(path, filename)
# This joins the path for each file it files so that python knows the full path / filename to trigger parser
tree = ET.parse(fullname)
# Parse the files..
# print(tree)
data = defaultdict(int)
# Get the root of the XML tree structure
root = tree.getroot()
# print(fullname)
for name in root.findall('.//name'):
data[name.text] += 1
print(data)

这是我得到的结果

defaultdict(<class 'int'>, { 'Damage': 3, 'Dent': 2})
defaultdict(<class 'int'>, {'Dent': 29, 'Damage': 7})
defaultdict(<class 'int'>, { 'Damage': 6, 'Dent': 15})
defaultdict(<class 'int'>, {'Damage': 7, 'Dent': 19})

我该怎么做才能得到这样的结果?

defaultdict(<class 'int'>, {'Damage': 23, 'Dent': 65})

我修改了你的代码,得到了你想要的结果。您可以尝试下面的代码片段:

import os
import xml.etree.ElementTree as ET
from collections import defaultdict
# files are in a sub folder where this script is being ran
path = r"D:Non_Documentsxml"
data_list = []
for filename in os.listdir(path):
# Only get xml files
if not filename.endswith('.xml'): continue
# I haven't been able to get it to work by just saying 'if filename.endswith('.xml')' only if not..
fullname = os.path.join(path, filename)
# This joins the path for each file it files so that python knows the full path / filename to trigger parser
tree = ET.parse(fullname)
# Parse the files..
# print(tree)
data = defaultdict(int)
# Get the root of the XML tree structure
root = tree.getroot()
# print(fullname)
for name in root.findall('.//name'):
data[name.text] += 1
data_list.append(data)
result = defaultdict(int)
for data in data_list:
for key, value in data.items():
result[key] += value
print(result)

输出:

defaultdict(<class 'int'>, {'Damage': 30, 'Dent': 20})

将所有的Dict添加到如下所示的列表中,

a = [defaultdict(<class 'int'>, { 'Damage': 3, 'Dent': 2}),
defaultdict(<class 'int'>, {'Dent': 29, 'Damage': 7}),
defaultdict(<class 'int'>, { 'Damage': 6, 'Dent': 15}),
defaultdict(<class 'int'>, {'Damage': 7, 'Dent': 19})]

使用下面的代码返回预期的输出,

sumDict = {'Damage': 0, 'Dent': 0}
for i in a:
for m,n in i.items():
sumDict.update({m:sumDict[m]+n})

输出将是,

{'Damage': 23, 'Dent': 65}

如果你喜欢伴侣就竖起大拇指!!