import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
for child in root:
print(child.tag)
print(child.attrib)
child.tag 将得到fileinfo,generalinfo,analysis,checksums
child.attrib 将得到
{'appname': 'Bruker Compass DataAnalysis', 'appversion': '5.1.1.225.3908', 'app_platform': '64-bit', 'date': '2019-01-18T13:07:20', 'type': 'DataAnalysis Results', 'typeversion': '1.1', 'subtype': 'MS Spectrum'}
generalinfo
{'organisation': '', 'user': 'admin', 'hostname': 'LCMPORTENI', 'os': 'Windows 10, Version 1803 (Enterprise)', 'architecture': '64-bit'}
analysis
{'name': 'D:\Data\2018\181029\A0-4M-pos_2952.d\analysis.baf'}
checksums
{}
#pk节点下的内容
for pks in root.iter('pk‘)
... print pks.attrib
{'mz': '59.29406217', 'i': '10048403.0', 'sn': '28.52', 'a': '628.86', 'fwhm': '0.00006897', 'res': '859671.1', 'algo': 'FTMS'}
{'mz': '59.29414023', 'i': '8744563.0', 'sn': '24.68', 'a': '484.49', 'fwhm': '0.00006751', 'res': '878295.6', 'algo': 'FTMS'}
{'mz': '59.38739496', 'i': '7075282.0', 'sn': '19.76', 'a': '375.88', 'fwhm': '0.00006126', 'res': '969366.8', 'algo': 'FTMS'}
{'mz': '59.38747322', 'i': '9047820.0', 'sn': '25.57', 'a': '539.82', 'fwhm': '0.00005741', 'res': '1034455.0', 'algo': 'FTMS'}
{'mz': '59.85876545', 'i': '29938584.0', 'sn': '87.11', 'a': '1622.06', 'fwhm': '0.00005638', 'res': '1061732.2', 'algo': 'FTMS'}
... print pks.get('mz')
59.29406217
59.29414023
59.38739496
59.38747322
59.85876545
59.85884723
59.85892732
59.85900491
60.08251764
61.26407292
61.26415901
61.26424307
61.26432800
61.26441165
#对每个节点 找特定的key text值
for country in root.findall('pk'):
... rank = country.find('mz').text
... name = country.get('name')
... print(name, rank)