我正在尝试修改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>
我的python尝试是:
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')
答案 0 :(得分:1)
问题是,对于fitid,你不需要元素的text
,你想要元素本身。因此,您不希望stmtrn.find('FITID').text
而是stmtrn.find('FITID')
。这将使您的作业fitid.text = ....
按预期工作。
你肯定不想做fitid = stmtrn.find('DTPOSTED')
,我认为这是你粘贴的代码中的拼写错误。
试试这段代码:
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')