daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Jan 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2.  
  3. import xml.dom.minidom
  4. import csv
  5. import glob
  6.  
  7. for file in glob.glob('CAMT.053*.xml'):
  8.     print(file)
  9.     camt = xml.dom.minidom.parse(file)
  10.     document = camt.documentElement
  11.     csvfile = open(file.replace('.xml','.csv'), 'w', newline='')
  12.     csvwriter = csv.writer(csvfile, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
  13.     csvwriter.writerow(['Date','Description','Amount'])
  14.  
  15.     balance_before = float(document.getElementsByTagName('Bal')[0].getElementsByTagName('Amt')[0].firstChild.data)
  16.     balance_after  = float(document.getElementsByTagName('Bal')[1].getElementsByTagName('Amt')[0].firstChild.data)
  17.     balance = 0
  18.  
  19.     for entry in document.getElementsByTagName('Ntry'):
  20.         name = entry.getElementsByTagName('AddtlNtryInf')[0].firstChild.data
  21.         date = entry.getElementsByTagName('BookgDt')[0].getElementsByTagName('Dt')[0].firstChild.data
  22.         for entry_detail in entry.getElementsByTagName('NtryDtls'):
  23.             for transaction in entry_detail.getElementsByTagName('TxDtls'):
  24.                 amount = transaction.getElementsByTagName('Amt')[0].firstChild.data
  25.                 direction = transaction.getElementsByTagName('CdtDbtInd')[0].firstChild.data
  26.                 if(direction == 'DBIT'): amount = float(amount)*-1
  27.  
  28.                 if(transaction.getElementsByTagName('Nm')):
  29.                     name = transaction.getElementsByTagName('Nm')[0].firstChild.data
  30.                 print(date + ' : ' + name + ' : ' + str(amount))
  31.                 csvwriter.writerow([date,name,str(amount)])
  32.                 balance = balance + float(amount)
  33.  
  34.     print(str(balance))
  35.     print(str(balance_before) + ' - ' + str(balance_after) + ' = ' + str(balance_after - balance_before))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top