Skip to content

Instantly share code, notes, and snippets.

@aimtiaz11
Created June 23, 2024 02:00
Show Gist options
  • Save aimtiaz11/05b5b4cc8a7fc9c46a998bdc276cc5f4 to your computer and use it in GitHub Desktop.
Save aimtiaz11/05b5b4cc8a7fc9c46a998bdc276cc5f4 to your computer and use it in GitHub Desktop.
Read and Parse Text file into CSV file
# Define the input and output file paths
# UPDATE this accordingly
input_file_path = 'raw-statement-data.txt'
output_file_path = 'csv-statement-data.csv'
def parse_transaction_line(line):
# Split the line into components - UPDATE this based on your line setup
parts = line.strip().split()
# Date is the first two parts
date = " ".join(parts[:2])
# Price is the last part
price = parts[-1]
# Reference number is the third last part
ref_no = parts[-2]
# Description is everything in between date and reference number
description = " ".join(parts[2:-2])
return date, description, ref_no, price
def read_transactions(file_path):
transactions = []
with open(file_path, 'r') as file:
for line in file:
if line.strip(): # Ensure it's not an empty line
date, description, ref_no, price = parse_transaction_line(line)
transactions.append((date, description, ref_no, price))
return transactions
def write_parsed_transactions(file_path, transactions):
with open(file_path, 'w') as file:
for transaction in transactions:
line = f"{transaction[0]},{transaction[1]},{transaction[2]},{transaction[3]}\n"
file.write(line)
# Read and parse the transactions
transactions = read_transactions(input_file_path)
# Write the parsed transactions to a new file
write_parsed_transactions(output_file_path, transactions)
print(f"Parsed transactions written to {output_file_path}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment