0

I have the following problem: I have an xml file which I am parsing through xml.etree.ElementTree which has the following structure

<e3r>
<moreData>false</moreData>
<data>
<CashDividend Isin="IE00BYM8JD58" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor=""/>
<CashDividend Isin="IE00BZ163G84" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" Amount_Status=""/>
<CashDividend Isin="IE00BZ163H91" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor=""/>
<CashDividend Isin="IE00BZ163M45 " AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" />
</data>
</e3r>

I am getting some data for the tag AdjustmentFactor trough an API (where I use the Isin to get the data) and what I want to achieve is to add data that I got with the API to AdjustmentFactor. (which is linked to the Isin). I somehow can't solve this.

This is how I get each ISIN:

isins = []

 for child in tree.getroot().getchildren()[1].getchildren():
            isins.append(child.attrib['Isin'])

 for isin in isins:
    print isin

But I don't know how to add data to AdjustmentFactor using the data stored in the list isins.

Can someone assist please ?

1 Answer 1

1

It isn't stated clearly in the question, but if I understand this correctly, you want to update AdjustmentFactor attribute value of an element where Isin attribute equals certain value. You can use XPath expression like .//element_name[@attribute_name='attribute_value'] to find element by its attribute value.

working demo example :

from xml.etree import ElementTree as et

raw = '''<e3r>
<moreData>false</moreData>
<data>
<CashDividend Isin="IE00BYM8JD58" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor=""/>
<CashDividend Isin="IE00BZ163G84" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" Amount_Status=""/>
<CashDividend Isin="IE00BZ163H91" AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor=""/>
<CashDividend Isin="IE00BZ163M45 " AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" />
</data>
</e3r>'''

root = et.fromstring(raw)
isin = "IE00BYM8JD58"
adjustment_factor = "FOO"
element = root.find(".//CashDividend[@Isin='%s']" % isin)
element.set("AdjustmentFactor", adjustment_factor)

print et.tostring(root)

eval.in demo

output :

<e3r>
<moreData>false</moreData>
<data>
<CashDividend AdjustmentFactor="FOO" Adjustment_Date="" Adjustment_Factor="" Isin="IE00BYM8JD58" />
<CashDividend AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" Amount_Status="" Isin="IE00BZ163G84" />
<CashDividend AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" Isin="IE00BZ163H91" />
<CashDividend AdjustmentFactor="" Adjustment_Date="" Adjustment_Factor="" Isin="IE00BZ163M45 " />
</data>
</e3r>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.