1

我正在尝试修改 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')
4

1 回答 1

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')
于 2013-08-30T22:56:08.513 回答