I'd like to create a python script that reads .CSV bank statements, that collates a list…
- User
I'd like to create a python script that reads .CSV bank statements, that collates a list of people I have paid money to and the amount of money I have paid to them in the statement. I would like that data to be saved into a new .CSV file. Could you write me some code that would do that?
- Assistant
Sure! Here's a script that should do what you're asking for:
import csv def read_csv(filename): with open(filename, 'r') as file: reader = csv.reader(file) header = next(reader) transactions = [] for row in reader: transactions.append(row) return header, transactions def collate_data(header, transactions): payees = {} payee_index = header.index('Payee') amount_index = header.index('Amount') for transaction in transactions: payee = transaction[payee_index] amount = float(transaction[amount_index]) if payee in payees: payees[payee] += amount else: payees[payee] = amount return payees def save_to_csv(payees, filename): with open(filename, 'w', newline='') as file: writer = csv.writer(file) writer.writerow(['Payee', 'Amount']) for payee, amount in payees.items(): writer.writerow([payee, amount]) if __name__ == '__main__': header, transactions = read_csv('input.csv') payees = collate_data(header, transactions) save_to_csv(payees, 'output.csv') - User
Thanks you. This is very helpful!
- Assistant
Thanks too!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)