我正在尝试修改 OFX 导出文件中的 xml 字段。导出的生产者不会为文件中的 fitid 生成唯一键,因此我想用 xml 中的其他两个字段生成的唯一键替换该字段。
我已经了解了以下代码,但不知道如何编写新的 FITID 字段。
XML 文件的示例是:
<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>
我的蟒蛇尝试是:
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')