1

I'm trying to modify an xml field in an OFX export file. The producer of the export doesn't produce a unique key for the fitid filed in the file, so I want to replace that field with a unique key generated from two other fields in the xml.

I've got as far as the following code, but can't work out how to write the new FITID field.

A sample of the XML file is:

    <BANKTRANLIST>
                <DTSTART>20130705</DTSTART>
                <DTEND>20130805</DTEND>
                <STMTTRN>
                    <TRNTYPE>DEBIT</TRNTYPE>
                    <DTPOSTED>20130708</DTPOSTED>
                    <TRNAMT>-7.99</TRNAMT>
                    <FITID>08072013660</FITID>
                    <NAME>HARE HATCH SHEEPLANDS    </NAME>
                    <MEMO>Effective date: 06/07/2013</MEMO>
                </STMTTRN>
                <STMTTRN>
                    <TRNTYPE>DEBIT</TRNTYPE>
                    <DTPOSTED>20130708</DTPOSTED>
                    <TRNAMT>-6.75</TRNAMT>
                    <FITID>08072013660</FITID>
                    <NAME>BINGHAMS BREWERY LIMIT   </NAME>
                    <MEMO>Effective date: 06/07/2013</MEMO>
                </STMTTRN>
                <STMTTRN>
                    <TRNTYPE>DEBIT</TRNTYPE>
                    <DTPOSTED>20130709</DTPOSTED>
                    <TRNAMT>-282.5</TRNAMT>
                    <FITID>09072013660</FITID>
                    <NAME>WWW.DVLA.GOV.UK          </NAME>
                    <MEMO>Effective date: 08/07/2013</MEMO>
                </STMTTRN>
                <STMTTRN>
                    <TRNTYPE>DEBIT</TRNTYPE>
                    <DTPOSTED>20130715</DTPOSTED>
                    <TRNAMT>-84.78</TRNAMT>
                    <FITID>15072013660</FITID>
                    <NAME>BP TWYFORD CONNECT       </NAME>
                    <MEMO>Effective date: 12/07/2013</MEMO>
                </STMTTRN>
                <STMTTRN>
                    <TRNTYPE>DEBIT</TRNTYPE>
                    <DTPOSTED>20130715</DTPOSTED>
                    <TRNAMT>-25.1</TRNAMT>
                    <FITID>15072013660</FITID>
                    <NAME>WHITE HART SHERFIE       </NAME>
                    <MEMO>Effective date: 13/07/2013</MEMO>
                </STMTTRN>
    </BANKTRANLIST>

My python attempt is:

    from xml.etree import ElementTree as et

    datafile = '/Volumes/Data/Projects/moneydance fix/statement20130805.ofx'

    tree = et.parse(datafile)
    root = tree.getroot()
    for stmtrn in root.iter('STMTTRN'):
    amount = stmtrn.find('TRNAMT').text
    date = stmtrn.find('DTPOSTED').text
    fitid = stmtrn.find('DTPOSTED').text
    print "amount: ", (amount.split('.')[0])[1:]
    amount = (amount.split('.')[0])[1:]

    fitid.text = (date + amount).ljust(12,'0')
    print 'New fitid: ', fitid

    tree.write(datafile+'new')
1
  • Can you show the output of your code? Commented Aug 30, 2013 at 21:10

1 Answer 1

1

The problem is that for fitid, you don't want the text of the element, you want the element itself. So, you don't want to do stmtrn.find('FITID').text but rather stmtrn.find('FITID'). This will make your assignment, fitid.text = .... work as expected.

And you most certainly don't want to do fitid = stmtrn.find('DTPOSTED') which I believe is a typo in the code you pasted.

Try this code:

from xml.etree import ElementTree as et

datafile = 'statement20130805.ofx'

tree = et.parse(datafile)
root = tree.getroot()
for stmtrn in root.iter('STMTTRN'):
  amount = stmtrn.find('TRNAMT').text
  date = stmtrn.find('DTPOSTED').text

  print "amount: ", (amount.split('.')[0])[1:]
  amount = (amount.split('.')[0])[1:]

  fitid = stmtrn.find('FITID')
  fitid.text = (date + amount).ljust(12,'0')
  print 'New fitid: ', fitid.text

tree.write(datafile+'new')
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @audiodude, that works the way I expected (or rather wanted). And yes, it was a typo in my code. Long night...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.