xml = '''
<?xml version='1.0' encoding='UTF-8'?>
<dataDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.fpml.org/FpML-5/confirmation"
fpmlVersion="5-6">
<trade>
<tradeheader>
<partyTradeIdentifier>
<partyReference href="party1"/>
<tradeId tradeIdScheme="http://www.partyA.com/swaps/trade-id">TW9235</tradeId>
</partyTradeIdentifier>
</tradeheader>
<swap/>
</trade>
<party id="party1">
<partyID>PARTYAUS33</partyID>
<partyName>Party A</partyName>
</party>
<party id="party2">
<partyID>BARCGB2L</partyID>
<partyName>Party B</partyName>
</party>
</dataDocument>
'''
from lxml import objectify
from lxml import etree
root = objectify.fromstring(xml)
newparties = ['Party C','Party D']
for e in root.party:
print (root.partyName)
我想取代PartyNames甲方和乙方 分别与C方和D方。
答案 0 :(得分:0)
newparties = ['Party C','Party D']
for e,n in zip(root.party, newparties):
e.partyName._setText(n)
print etree.tostring(root, pretty_print=True)
输出:
$ python x.py
<dataDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.fpml.org/FpML-5/confirmation" fpmlVersion="5-6">
<trade>
<tradeheader>
<partyTradeIdentifier>
<partyReference href="party1"/>
<tradeId tradeIdScheme="http://www.partyA.com/swaps/trade-id">TW9235</tradeId>
</partyTradeIdentifier>
</tradeheader>
<swap/>
</trade>
<party id="party1">
<partyID>PARTYAUS33</partyID>
<partyName>Party C</partyName>
</party>
<party id="party2">
<partyID>BARCGB2L</partyID>
<partyName>Party D</partyName>
</party>
</dataDocument>