Repository navigation
Expand file tree
/
Copy pathcode.txt
More file actions
66 lines (55 loc) · 1.89 KB
/
Copy pathcode.txt
File metadata and controls
66 lines (55 loc) · 1.89 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import csv
import datetime
# ICICI-Input-Case2.csv HDFC-Input-Case1.csv
with open('ICICI-Input-Case2.csv', newline='') as csvFile:
obj = csv.DictReader(csvFile)
reader = csv.reader(csvFile)
outwDate = []
transaction = obj.fieldnames[1].split()[0]
for row in reader:
if row[0] == '' and row[1]:
last = row[1].split(' ')[-1]
if last.capitalize() == 'Transactions':
transaction = row[1].split()[0]
else:
cardName = row[1]
if transaction.capitalize() == 'Domestic':
currency = 'INR'
if row[0] and row[0][:4] != 'Date':
date = row[0]
trDes = row[1]
location = row[1].split()[-1].lower()
if transaction.capitalize() == 'International':
trDes = ' '.join(row[1].split()[:-1])
currency = row[1].split()[-1]
location = row[1].split()[-2].lower()
if len(row) == 3:
if row[2].split()[-1] == 'cr':
credit = row[2].split()[0]
debit = 0
else:
credit = 0
debit = row[2].split()[0]
elif len(row) == 4:
if row[3]:
credit = row[3]
debit = 0
else:
debit = row[2]
credit = 0
dateList = [int(_) for _ in date.split('-')]
dateObj = datetime.datetime(dateList[2], dateList[1], dateList[0])
# print(dateObj)
outRow = (date, trDes, debit, credit, currency,
cardName, transaction, location)
outwDate.append([dateObj, outRow, transaction])
outwDate.sort(key=lambda date: len(date[2]), reverse=True)
outwDate.sort(key=lambda date: date[0])
with open('output_case2.csv', 'w', newline='') as outFile:
fields = ['Date', 'Transaction Description', 'Debit',
'Credit', 'Currency', 'Card Name', 'Transaction', 'Location']
obj = csv.DictWriter(outFile, fieldnames=fields)
obj.writeheader()
writer = csv.writer(outFile)
for row in outwDate:
writer.writerow(row[1])