Import and sort data set
This commit is contained in:
commit
5e358b058d
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
.vscode/
|
||||||
|
bankView/
|
||||||
|
*.lnk
|
||||||
|
*.csv
|
||||||
|
*.db
|
||||||
|
__*__
|
123
bankView.py
Normal file
123
bankView.py
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#BankView - Calculate and Build Banking Stats
|
||||||
|
#Cal.W 2020
|
||||||
|
|
||||||
|
import csv
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
TRANSACTION_TYPES = ['Other', "Tap and Pay", "Card", "Deposit"]
|
||||||
|
|
||||||
|
#transDetails, transDate, transType, transAcc
|
||||||
|
def transDetails(transDetails):
|
||||||
|
transAcc = None
|
||||||
|
transType = TRANSACTION_TYPES[0]
|
||||||
|
transDetails = " ".join(transDetails.split())
|
||||||
|
transDate = None
|
||||||
|
|
||||||
|
z = transDetails.split(" Value Date: ")
|
||||||
|
if len(z) >= 2:
|
||||||
|
transDate = z[1]
|
||||||
|
transDetails = z[0]
|
||||||
|
|
||||||
|
#Don't Check the first (default) or last ()
|
||||||
|
for x in TRANSACTION_TYPES[1:-1]:
|
||||||
|
y = transDetails.split(" "+x+" ")
|
||||||
|
if len(y) >= 2:
|
||||||
|
transType = x
|
||||||
|
transAcc = y[1]
|
||||||
|
transDetails = y[0]
|
||||||
|
break
|
||||||
|
|
||||||
|
return transDetails, transDate, transType, transAcc
|
||||||
|
|
||||||
|
#Return a list of transactions in the form:
|
||||||
|
#(date(Transaction Date), date(Initialized), float(Amount), str(Details), float(Acc Balance), str(Payment Type), str(Acc))
|
||||||
|
def importTransactionData(fileName="import.csv", delimiter=',', newline=''):
|
||||||
|
transactions = list()
|
||||||
|
rawTransactions = list()
|
||||||
|
|
||||||
|
with open(fileName, newline=newline) as importCSVFile:
|
||||||
|
importCSV = csv.reader(importCSVFile, delimiter=delimiter)
|
||||||
|
rawTransactions = [x[:4] for x in importCSV]
|
||||||
|
|
||||||
|
#(date(Transaction Date), date(Initialized), float(Amount), str(Details), float(Acc Balance), str(Payment Type), str(Acc))
|
||||||
|
for x in rawTransactions:
|
||||||
|
y = transDetails(x[2])
|
||||||
|
transactions.append(
|
||||||
|
(
|
||||||
|
datetime.strptime(x[0], "%d/%m/%Y"),
|
||||||
|
datetime.strptime(
|
||||||
|
y[1] if y[1] else x[0],
|
||||||
|
"%d/%m/%Y"
|
||||||
|
),
|
||||||
|
float(str(x[1]).replace(",", "")),
|
||||||
|
str(y[0]),
|
||||||
|
float(str(x[3]).replace(",", "")),
|
||||||
|
TRANSACTION_TYPES[-1] if y[2] == TRANSACTION_TYPES[0] and float(str(x[1]).replace(",", "")) > 0 else str(y[2]),
|
||||||
|
str(y[3]) if y[3] else ""
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
#Filter out any duplicates - the 2 lines are the same (logic wise) as the 8 under it
|
||||||
|
seen = set()
|
||||||
|
transactions = [x for x in transactions if x not in seen and not seen.add(x)] #oh and the "not seen.add" is cause add always returns None
|
||||||
|
##seen = set()
|
||||||
|
##trans = []
|
||||||
|
##for x in transactions:
|
||||||
|
## if x not in seen:
|
||||||
|
## trans.append(x)
|
||||||
|
## seen.add(x)
|
||||||
|
## else:
|
||||||
|
## print(x)
|
||||||
|
##transactions = trans
|
||||||
|
|
||||||
|
transactions = sorted(transactions, reverse = True, key = lambda x: x[1]) #Sort from init date.
|
||||||
|
|
||||||
|
|
||||||
|
return transactions
|
||||||
|
|
||||||
|
|
||||||
|
def loadTransactionData(fileName="transData.csv"):
|
||||||
|
transactions = list()
|
||||||
|
|
||||||
|
with open(fileName, newline='') as importCSVFile:
|
||||||
|
importCSV = csv.reader(importCSVFile, delimiter=',')
|
||||||
|
transactions = [x[:7] for x in importCSV]
|
||||||
|
|
||||||
|
transactions = [
|
||||||
|
(
|
||||||
|
datetime.strptime(x[0], "%d/%m/%Y"),
|
||||||
|
datetime.strptime(str(x[1]), "%d/%m/%Y"),
|
||||||
|
float(x[2]),
|
||||||
|
str(x[3]),
|
||||||
|
float(x[4]),
|
||||||
|
str(x[5]),
|
||||||
|
str(x[6]) if len(x) > 6 else ""
|
||||||
|
) for x in transactions
|
||||||
|
]
|
||||||
|
|
||||||
|
transactions = sorted(transactions, reverse = True, key = lambda x: x[1]) #Sort from init date.
|
||||||
|
|
||||||
|
return transactions
|
||||||
|
|
||||||
|
def saveTransactionData(transactions, fileName="transData.csv"):
|
||||||
|
with open(fileName, 'w', newline='') as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerows([
|
||||||
|
(
|
||||||
|
x[0].strftime("%d/%m/%Y"),
|
||||||
|
x[1].strftime("%d/%m/%Y"),
|
||||||
|
x[2],
|
||||||
|
x[3],
|
||||||
|
x[4],
|
||||||
|
x[5],
|
||||||
|
x[6],
|
||||||
|
) for x in transactions
|
||||||
|
])
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
trans = importTransactionData() #importTransactionData("./Raw Transaction Data.csv")
|
||||||
|
[print(x) for x in trans if x[-2] == TRANSACTION_TYPES[0]]
|
||||||
|
saveTransactionData(trans)
|
0
requirements.txt
Normal file
0
requirements.txt
Normal file
Loading…
x
Reference in New Issue
Block a user