Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
[user]
I exported a CSV from a spreadsheet, but my budgeting app rejects it. Can you help reformat it?
[assistant]
Sure. Paste the first several lines of the CSV, including the header, and tell me what import format the app expects if it shows an example. Please redact account numbers or other sensitive values first.
[user]
Here’s the export. The app’s template has columns for Date, Description, Amount, and Category.
```text
Transaction Date,Payee,Debit,Credit,Notes
01/03/2026,Corner Market,42.18,,groceries
01/04/2026,Coffee Shop,,4.75,coffee
01/05/2026,Transit Reload,25.00,,transport
01/06/2026,Book Store,18.50,,books
```
I need one Amount column, with expenses negative and credits positive.
[assistant]
That can be converted by treating Debit as a negative amount and Credit as a positive amount. The output will be:
```csv
Date,Description,Amount,Category
2026-01-03,Corner Market,-42.18,groceries
2026-01-04,Coffee Shop,4.75,coffee
2026-01-05,Transit Reload,-25.00,transport
2026-01-06,Book Store,-18.50,books
```
I used ISO dates (`YYYY-MM-DD`), which many importers accept. If the app specifically requires `MM/DD/YYYY`, keep the original date style instead.
[user]
There are about 300 rows, so I’d rather automate it. I can use Python, but I’m not sure how to handle blank Debit or Credit cells.
[assistant]
Use the blank cell as zero, then calculate `Credit - Debit`. This script reads the source file and writes a new CSV:
```python
import csv
from decimal import Decimal
from datetime import datetime
source_file = "bank_export.csv"
output_file = "budget_import.csv"
with open(source_file, newline="", encoding="utf-8-sig") as src, \
open(output_file, "w", newline="", encoding="utf-8") as dst:
reader = csv.DictReader(src)
writer = csv.DictWriter(
dst,
fieldnames=["Date", "Description", "Amount", "Category"]
)
writer.writeheader()
for row in reader:
debit = Decimal((row["Debit"] or "0").replace(",", "").strip())
credit = Decimal((row["Credit"] or "0").replace(",", "").strip())
date = datetime.strptime(
row["Transaction Date"].strip(), "%m/%d/%Y"
).date().isoformat()
writer.writerow({
"Date": date,
"Description": row["Payee"].strip(),
"Amount": f"{credit - debit:.2f}",
"Category": row["Notes"].strip(),
})
```
Put the script in the same folder as `bank_export.csv`, then run:
```text
python convert_csv.py
```
It will create `budget_import.csv`.
[user]
Some rows have commas in the payee, and a few have a dollar sign in the amounts. Will the script handle those?
[assistant]
The CSV reader handles commas inside properly quoted fields, such as `"Market, Main Street"`. The current amount conversion handles commas but not dollar signs. Add a small helper that removes `$` before converting:
```python
def parse_amount(value):
value = (value or "").strip().replace("$", "").replace(",", "")
return Decimal(value or "0")
```
Then replace these two lines:
```python
debit = Decimal((row["Debit"] or "0").replace(",", "").strip())
credit = Decimal((row["Credit"] or "0").replace(",", "").strip())
```
with:
```python
debit = parse_amount(row["Debit"])
credit = parse_amount(row["Credit"])
```
For quoted payees, keep using `csv.DictReader`; do not split each line manually. For example, this input:
```csv
01/07/2026,"Market, Main Street",$12.50,,food
```
will produce a description of `Market, Main Street` and an amount of `-12.50`.
[user]
What if the export has refunds shown as negative values in the Credit column? I want the output to preserve the sign correctly rather than always treating Credit as positive.
[assistant]
If the source can contain signed values, the safest rule is to parse each nonblank field with its existing sign and add Credit to the negative of Debit only when Debit is an expense column. In other words:
- Debit `42.18` becomes `-42.18`.
- Credit `4.75` becomes `4.75`.
- Credit `-12.00` remains `-12.00`.
- Blank Debit and Credit become `0`.
Use this calculation:
```python
amount = credit - debit
```
That preserves a negative Credit value as negative. However, if the export uses negative Debit values too, normalize the columns first:
```python
debit_value = parse_amount(row["Debit"])
credit_value = parse_amount(row["Credit"])
debit = abs(debit_value)
credit = credit_value
amount = credit - debit
```
Only use the `abs()` version if Debit is consistently an expense column and may occasionally carry a minus sign. Otherwise, retain the first version so the source signs are not changed unexpectedly.
[user]
Could you give me a complete script with validation? I’d like it to report rows with missing dates, invalid amounts, or both Debit and Credit filled in.
[assistant]
Here is a complete version. It skips invalid rows, writes them to `conversion_errors.csv`, and reports totals at the end.
```python
import csv
from decimal import Decimal, InvalidOperation
from datetime import datetime
SOURCE = "bank_export.csv"
OUTPUT = "budget_import.csv"
ERRORS = "conversion_errors.csv"
def parse_amount(value):
text = (value or "").strip().replace("$", "").replace(",", "")
if text == "":
return Decimal("0.00")
return Decimal(text)
def parse_date(value):
text = (value or "").strip()
if not text:
raise ValueError("missing date")
return datetime.strptime(text, "%m/%d/%Y").date().isoformat()
converted = 0
errors = []
with open(SOURCE, newline="", encoding="utf-8-sig") as src:
reader = csv.DictReader(src)
with open(OUTPUT, "w", newline="", encoding="utf-8") as dst:
writer = csv.DictWriter(
dst,
fieldnames=["Date", "Description", "Amount", "Category"]
)
writer.writeheader()
for line_number, row in enumerate(reader, start=2):
problems = []
try:
date = parse_date(row.get("Transaction Date"))
except (ValueError, TypeError):
date = ""
problems.append("invalid or missing date")
try:
debit_text = (row.get("Debit") or "").strip()
credit_text = (row.get("Credit") or "").strip()
if debit_text and credit_text:
problems.append("both Debit and Credit are filled")
debit = parse_amount(debit_text)
credit = parse_amount(credit_text)
amount = credit - debit
except (InvalidOperation, ValueError):
amount = Decimal("0.00")
problems.append("invalid amount")
if problems:
errors.append({
"Line": line_number,
"Problems": "; ".join(problems),
"SourceRow": repr(row),
})
continue
writer.writerow({
"Date": date,
"Description": (row.get("Payee") or "").strip(),
"Amount": f"{amount:.2f}",
"Category": (row.get("Notes") or "").strip(),
})
converted += 1
with open(ERRORS, "w", newline="", encoding="utf-8") as err:
writer = csv.DictWriter(err, fieldnames=["Line", "Problems", "SourceRow"])
writer.writeheader()
writer.writerows(errors)
print(f"Converted rows: {converted}")
print(f"Rows with errors: {len(errors)}")
print(f"Output file: {OUTPUT}")
print(f"Error file: {ERRORS}")
```
Run it with:
```text
python convert_csv.py
```
Example output:
```text
Converted rows: 294
Rows with errors: 6
Output file: budget_import.csv
Error file: conversion_errors.csv
```
Review `conversion_errors.csv`, correct those source rows, and rerun the script. Because the script overwrites the output files, each run starts cleanly.
[user]
I ran it and got an error: `KeyError: 'Debit'`. The header in my actual file is `Withdrawal` instead of `Debit`, and `Deposit` instead of `Credit`.
[assistant]
Change the two column lookups to use your actual headers:
```python
debit_text = (row.get("Withdrawal") or "").strip()
credit_text = (row.get("Deposit") or "").strip()
```
You can also make the names configurable near the top:
```python
DEBIT_COLUMN = "Withdrawal"
CREDIT_COLUMN = "Deposit"
```
Then use:
```python
debit_text = (row.get(DEBIT_COLUMN) or "").strip()
credit_text = (row.get(CREDIT_COLUMN) or "").strip()
```
The rest of the script can stay the same. If you want it to fail early when a header is misspelled, add this after creating the reader:
```python
required = {
"Transaction Date",
"Payee",
DEBIT_COLUMN,
CREDIT_COLUMN,
"Notes",
}
missing = required - set(reader.fieldnames or [])
if missing:
raise ValueError(
f"Missing required columns: {', '.join(sorted(missing))}"
)
```
For your file, the expected header set is:
```text
Transaction Date,Payee,Withdrawal,Deposit,Notes
```
Quoted commas in payees will still be handled by `csv.DictReader`.
[assistant]
A minor improvement is to strip whitespace from header names too, since some exports produce headers like `Withdrawal `:
```python
reader = csv.DictReader(src)
reader.fieldnames = [
name.strip() if name else name
for name in (reader.fieldnames or [])
]
```
Place that immediately after `reader = csv.DictReader(src)`, before the required-column check. Then use the configurable `DEBIT_COLUMN` and `CREDIT_COLUMN` values as shown above.