DO NOT TRY TO copy this text and paste it in a file to run. Python, sadly, relies on tabification to indicate membership in conditional statements. pasting here messes that up and potentially loses intent.
=====<[some text]>=====
indicates a section comment and is NOT a python acceptable comment. I'm just using that to make it stand out here
Here's the text of the script:
=====<[This part reads in libraries]>=====
# Written by Wayne L. Contello
# No guarantee as to the fitness of the script to be used for the intended purpose or any other purpose
# copyright 2019
# All Rights reserved
from __future__ import print_function
import builtins
# import pylab
# import numpy as np
# import matplotlib.pyplot as plt
import ntpath
import tkinter as tk
# import tkMessageBox
import re
import time
import re
from decimal import *
from tkinter import *
from tkinter import filedialog
# from tkinter import *
# import tkFileDialog
# import FileDialog
root = tk.Tk();
root.withdraw();
=====<[This prompts to select the input QXF file. There is no checking the file type, so select the correct file and do not mess up]>=====
file = tk.filedialog.askopenfile(parent=root);
print ("Input file: " + file.name + "\n");
# print("There are %d data items" % (len(DataItem)))
=====<[This sets the output file name and path, then opens the file]>=====
oFileName = file.name.replace(" ", "_") + ".out.txt"
print("Opening file: " + oFileName)
oFile = open(oFileName, 'w');
oFile.write("")
# now read the data from the input file and write to the proper output file if the CAN ID matches
x = 0
linesOfFile = file.read().split("<STMTTRN>")
for line in linesOfFile:
# this parses the fields out of a single line in the file
if(x > 0):
row = {}
# TRNTYPE = ""
# DTPOSTED = ""
# TRNAMT = ""
# FITID = ""
# NAME = ""
# MEMO = ""
# CHECKNUM = ""
# NAME = ""
remainingline = line.replace("</STMTTRN>", "")
# print(remainingline)
result = re.split("<", remainingline)
for item in result:
if(item):
fields = item.split('>')
row[fields[0]] = fields[1]
# print(row)
# Amount Date Type Check Number Payee Name Memo
# TRNAMT DTPOSTED TRNTYPE CHECKNUM NAME MEMO
oStr = ""
val = row.get('TRNAMT') if row.get('TRNAMT') else ""
oStr += val
oStr += "\t"
val = row.get('DTPOSTED') if row.get('DTPOSTED') else ""
if(val):
DateTime = val.split('.')
# print(DateTime[0])
YYYY = DateTime[0][0:4]
Mon = DateTime[0][4:6]
DD = DateTime[0][6:8]
HH = DateTime[0][8:10]
MM = DateTime[0][10:12]
SS = DateTime[0][12:14]
DT = Mon+"/"+DD+"/"+YYYY+" "+HH+":"+MM+":"+SS
else:
DT = ""
oStr += DT
oStr += "\t"
val = row.get('TRNTYPE') if row.get('TRNTYPE') else ""
oStr += val
oStr += "\t"
val = row.get('CHECKNUM') if row.get('CHECKNUM') else ""
oStr += val
oStr += "\t"
val = row.get('NAME') if row.get('NAME') else ""
oStr += val
oStr += "\t"
val = row.get('MEMO') if row.get('MEMO') else ""
oStr += val
# this write the reformatted line to the output file
# print("%d:\t%s" %(x, oStr))
oFile.write("%s\n" % (oStr))
x += 1
print("%08d" % (x))
# close the open files
if(oFile) :
oFile.close()
print("closing output file")
if(file) :
file.close()
print("closing input file")
exit();