-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_parser.py
62 lines (49 loc) · 1.92 KB
/
transaction_parser.py
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
import re
from datetime import datetime
class Transaction:
def __init__(self, date, client, amount):
self.date = date # datetime object
self.client = client
self.amount = amount
def filter_transactions(text):
'''Takes text and Returns a list of Transaction objects'''
text_list = text.splitlines() # Contains list of lines
# Define the pattern to match the structure
pattern = r'(\d{2}/\d{2}/\d{4})\s+(.+)\s+(\d+\.\d+)'
transactions = []
# Filter lines that match the pattern
for line in text_list:
match = re.match(pattern, line)
if match:
# Extract date, name, and number from the match object
date = match.group(1)
date = datetime.strptime(date, "%m/%d/%Y")
client = match.group(2)
amount = float(match.group(3))
transactions.append(Transaction(date, client, amount))
return transactions
def total_transactional_cost(transactions):
total_amount = 0.0
for transaction in transactions:
total_amount += transaction.amount
return total_amount
def transactional_cost_timeframe(transactions, initial_date, end_date):
# both input dates are string
total_amount = 0.0
initial_date = datetime.strptime(initial_date, "%m/%d/%Y")
end_date = datetime.strptime(end_date, "%m/%d/%Y")
for transaction in transactions:
if initial_date <= transaction.date <= end_date:
total_amount += transaction.amount
return total_amount
text_path = "Output/Text/pdf_text.txt"
text = ""
with open(text_path, "r") as file:
text = file.read()
transactions = filter_transactions(text)
for transaction in transactions:
print(f"{transaction.date} {transaction.client} {transaction.amount}")
print(total_transactional_cost(transactions))
initial_date = "02/11/2021"
end_date = "02/11/2023"
print(transactional_cost_timeframe(transactions, initial_date, end_date))