-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqfx2csv.py
More file actions
50 lines (38 loc) · 1.93 KB
/
Copy pathqfx2csv.py
File metadata and controls
50 lines (38 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from datetime import datetime
import xml.etree.ElementTree as ET
import csv
import argparse
def extract_fields(xml_file, csv_file):
# Parse the XML file
tree = ET.parse(xml_file)
root = tree.getroot()
# Extract the ACCTID
acct_id = root.find('.//ACCTID').text if root.find('.//ACCTID') is not None else ""
# Open the CSV file in write mode with UTF-8 encoding
with open(csv_file, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['Account ID', 'Transaction ID',' Transaction Type', 'Transaction Date', 'Description', 'Date Posted', 'Description', 'Card Used'])
# Iterate through each STMTTRN element
for stmttrn in root.findall('.//STMTTRN'):
# Extract required fields
trntype = stmttrn.find('TRNTYPE').text
dtposted = datetime.strptime(stmttrn.find('DTPOSTED').text[:-10], '%Y%m%d')
trnamt = stmttrn.find('TRNAMT').text
fitid = stmttrn.find('FITID').text
memo = stmttrn.find('MEMO').text
# Fix date for better importing to spreadsheet
dtuser = datetime.strptime(stmttrn.find('DTUSER').text[:-10], '%Y%m%d') if stmttrn.find('DTUSER') is not None else dtposted
ccacctto_acctid = stmttrn.find('.//CCACCTTO/ACCTID').text if stmttrn.find('.//CCACCTTO/ACCTID') is not None else ""
# Write the fields to the CSV file
writer.writerow([acct_id, fitid, trntype, dtuser.date(), dtposted.date(),trnamt, memo, ccacctto_acctid])
def main():
# Parse command-line arguments
parser = argparse.ArgumentParser(description='Extract specified fields from XML and convert to CSV')
parser.add_argument('input', help='Input XML file')
parser.add_argument('output', help='Output CSV file')
args = parser.parse_args()
# Extract fields and write to CSV
extract_fields(args.input, args.output)
print("QFX has been converted to CSV")
if __name__ == "__main__":
main()