https://www.cnblogs.com/hupeng1234/p/7262371.
import xml.etree.ElementTree as ET tree = ET.parse("D:\store.txt") root = tree.getroot() dataDict = {} for child in root: print(child.tag) tempList = [] tempList.append(child[2].text) tempList.append(child[7].text) if child[6].text == "合格": tempList.append("Y") else: tempList.append('N') dataKey = '-'.join(tempList) value = child[5].text flag = True for key in dataDict: if key == dataKey: value = value + dataDict[dataKey] dataDict[dataKey] = value flag = False if flag: dataDict[dataKey] = value print(dataDict)