Python: ElementTree 中的多重打印
Python: Multiple printing in ElementTree
我有多个地址和设置名称,如下面 xml 代码所示:
<Settings>
<Setting>
<Address>15:0</Address>
<Comments />
<Conditions />
<IsSingleEnded>true</IsSingleEnded>
<IsVisibilityExternal>false</IsVisibilityExternal>
<MetaDatas />
<SettingName>123</SettingName>
</Setting>
<Setting>
<Address>18:0</Address>
<Comments />
<Conditions />
<IsSingleEnded>true</IsSingleEnded>
<IsVisibilityExternal>false</IsVisibilityExternal>
<MetaDatas />
<SettingName>abc</SettingName>
</Setting>
</Settings>
我想打印所有的设置名称和地址。
这是我的代码:
from xml.etree import ElementTree
with open('abc.xml', 'rt') as f:
tree = ElementTree.parse(f)
for name in tree.findall('.//SettingName'):
for address in tree.findall('.//Address'):
name1 = name.text
address1 = address.text
if name1 and address1:
print name1, address1
最后打印了多次settingname和address。我想要的是一个设置名称有一个地址,它会一直打印直到完成所有设置名称的打印。
我可以知道如何通过我的代码修复它吗?
您需要获取所有 Setting
节点,然后在其中迭代以获取每个设置的每个 SettingName
和 Address
节点并打印它们。示例 -
from xml.etree import ElementTree
with open('BXT_A0_RevF15.xml', 'rt') as f:
tree = ElementTree.parse(f)
for setting in tree.findall('.//Setting'):
address = setting.find('./Address').text
setting_name = setting.find('./SettingName').text
if setting_name and address:
print setting_name , address
如果某些设置可能没有 Address
或 SettingName
,那么您还需要通过检查 None
来考虑这些情况。示例 -
from xml.etree import ElementTree
with open('BXT_A0_RevF15.xml', 'rt') as f:
tree = ElementTree.parse(f)
for setting in tree.findall('.//Setting'):
address = setting.find('./Address')
setting_name = setting.find('./SettingName')
if setting_name is not None and address is not None:
print setting_name.text , address.text
我有多个地址和设置名称,如下面 xml 代码所示:
<Settings>
<Setting>
<Address>15:0</Address>
<Comments />
<Conditions />
<IsSingleEnded>true</IsSingleEnded>
<IsVisibilityExternal>false</IsVisibilityExternal>
<MetaDatas />
<SettingName>123</SettingName>
</Setting>
<Setting>
<Address>18:0</Address>
<Comments />
<Conditions />
<IsSingleEnded>true</IsSingleEnded>
<IsVisibilityExternal>false</IsVisibilityExternal>
<MetaDatas />
<SettingName>abc</SettingName>
</Setting>
</Settings>
我想打印所有的设置名称和地址。 这是我的代码:
from xml.etree import ElementTree
with open('abc.xml', 'rt') as f:
tree = ElementTree.parse(f)
for name in tree.findall('.//SettingName'):
for address in tree.findall('.//Address'):
name1 = name.text
address1 = address.text
if name1 and address1:
print name1, address1
最后打印了多次settingname和address。我想要的是一个设置名称有一个地址,它会一直打印直到完成所有设置名称的打印。 我可以知道如何通过我的代码修复它吗?
您需要获取所有 Setting
节点,然后在其中迭代以获取每个设置的每个 SettingName
和 Address
节点并打印它们。示例 -
from xml.etree import ElementTree
with open('BXT_A0_RevF15.xml', 'rt') as f:
tree = ElementTree.parse(f)
for setting in tree.findall('.//Setting'):
address = setting.find('./Address').text
setting_name = setting.find('./SettingName').text
if setting_name and address:
print setting_name , address
如果某些设置可能没有 Address
或 SettingName
,那么您还需要通过检查 None
来考虑这些情况。示例 -
from xml.etree import ElementTree
with open('BXT_A0_RevF15.xml', 'rt') as f:
tree = ElementTree.parse(f)
for setting in tree.findall('.//Setting'):
address = setting.find('./Address')
setting_name = setting.find('./SettingName')
if setting_name is not None and address is not None:
print setting_name.text , address.text