Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
[user]
I have a CSV export from a bank-like transaction list, and my budgeting app rejects it. Can you help me reformat it?
[assistant]
Sure. Paste a representative sample, including the header and a few rows. You can replace account numbers, descriptions, and amounts with dummy values if needed. Also tell me the budgeting app’s required column names or show its import template, if you have one.
[user]
Here is the export. It has some extra columns and uses a date format I suspect the app dislikes:
```text
Transaction Date,Post Date,Description,Reference,Debit,Credit,Balance,Category
01/03/2026,01/04/2026,Corner Shop,ABC123,12.50,,987.50,Groceries
01/05/2026,01/06/2026,Bus Fare,ABC124,2.75,,984.75,Transport
01/07/2026,01/08/2026,Refund,,,"15.00",999.75,Other
```
The app’s sample template has these columns:
```text
Date,Payee,Amount,Category,Notes
```
[assistant]
That gives us the target structure. The main changes are:
- Keep `Transaction Date` as `Date`.
- Copy `Description` to `Payee`.
- Combine `Debit` and `Credit` into one signed `Amount` column. A common convention is expenses as negative and credits as positive, but the app may use the reverse.
- Copy `Category`.
- Put `Reference` in `Notes`, or leave `Notes` blank.
- Drop `Post Date` and `Balance`.
Using expenses as negative and credits as positive, the converted data would be:
```csv
Date,Payee,Amount,Category,Notes
2026-01-03,Corner Shop,-12.50,Groceries,ABC123
2026-01-05,Bus Fare,-2.75,Transport,ABC124
2026-01-07,Refund,15.00,Other,
```
Before importing a large file, check whether the app accepts ISO dates (`YYYY-MM-DD`) and whether it expects expenses to be negative. Those are the two most likely points of failure.
[user]
I checked the help text. It says dates must be `YYYY-MM-DD`, amounts must be positive, and there is a separate `Type` column with values `Expense` or `Income`. The template is actually:
```text
Date,Payee,Amount,Type,Category,Notes
```
[assistant]
Then the output should use the absolute value of the debit or credit as `Amount`, and classify each row based on which source column contains a value:
```csv
Date,Payee,Amount,Type,Category,Notes
2026-01-03,Corner Shop,12.50,Expense,Groceries,ABC123
2026-01-05,Bus Fare,2.75,Expense,Transport,ABC124
2026-01-07,Refund,15.00,Income,Other,
```
For a complete conversion, use this mapping:
| Output column | Source or rule |
|---|---|
| `Date` | `Transaction Date`, converted to `YYYY-MM-DD` |
| `Payee` | `Description` |
| `Amount` | Nonblank `Debit` or `Credit`, without a sign |
| `Type` | `Expense` when `Debit` is nonblank; otherwise `Income` |
| `Category` | `Category` |
| `Notes` | `Reference` |
Do not include `Balance` or `Post Date`. Also avoid thousands separators in amounts unless the app specifically documents support for them.
[user]
I have about 400 rows, so I would rather automate it. I can use Python, but I am not sure how to handle quoted commas, blank debit cells, and dates consistently.
[assistant]
Python’s built-in `csv` module will handle quoted commas safely. Here is a script that reads `bank_export.csv` and writes `budget_import.csv`:
```python
import csv
from datetime import datetime
from decimal import Decimal
INPUT = "bank_export.csv"
OUTPUT = "budget_import.csv"
def parse_amount(value, row_number, field_name):
value = value.strip()
if not value:
return None
value = value.replace(",", "")
try:
amount = Decimal(value)
except Exception as exc:
raise ValueError(
f"Row {row_number}: invalid {field_name} amount {value!r}"
) from exc
if amount < 0:
amount = abs(amount)
return amount
with open(INPUT, "r", newline="", encoding="utf-8-sig") as source, \
open(OUTPUT, "w", newline="", encoding="utf-8") as target:
reader = csv.DictReader(source)
required = {
"Transaction Date", "Description", "Debit", "Credit",
"Reference", "Category"
}
missing = required - set(reader.fieldnames or [])
if missing:
raise ValueError(f"Missing input columns: {sorted(missing)}")
writer = csv.DictWriter(
target,
fieldnames=["Date", "Payee", "Amount", "Type", "Category", "Notes"]
)
writer.writeheader()
for row_number, row in enumerate(reader, start=2):
raw_date = row["Transaction Date"].strip()
try:
date = datetime.strptime(raw_date, "%m/%d/%Y").date()
except ValueError as exc:
raise ValueError(
f"Row {row_number}: invalid date {raw_date!r}; "
"expected MM/DD/YYYY"
) from exc
debit = parse_amount(row["Debit"], row_number, "Debit")
credit = parse_amount(row["Credit"], row_number, "Credit")
if (debit is None) == (credit is None):
raise ValueError(
f"Row {row_number}: exactly one of Debit or Credit "
"must contain an amount"
)
if debit is not None:
amount = debit
transaction_type = "Expense"
else:
amount = credit
transaction_type = "Income"
writer.writerow({
"Date": date.isoformat(),
"Payee": row["Description"].strip(),
"Amount": f"{amount:.2f}",
"Type": transaction_type,
"Category": row["Category"].strip(),
"Notes": row["Reference"].strip(),
})
print(f"Wrote {OUTPUT}")
```
This deliberately stops on a malformed row instead of silently producing an import file with questionable data.
[user]
That looks good. My actual export sometimes has dates like `1/3/2026`, and sometimes the debit or credit values contain a dollar sign. Can the script accept both date padding styles and currency symbols?
[assistant]
Yes. `datetime.strptime` accepts single-digit month and day values with `%m/%d/%Y` on common Python versions, but we can make date parsing explicit and remove currency symbols more carefully. Replace the parsing portions with this version:
```python
from datetime import datetime
from decimal import Decimal, InvalidOperation
def parse_date(value, row_number):
value = value.strip()
for date_format in ("%m/%d/%Y", "%Y-%m-%d"):
try:
return datetime.strptime(value, date_format).date()
except ValueError:
pass
raise ValueError(
f"Row {row_number}: invalid date {value!r}; "
"expected M/D/YYYY or YYYY-MM-DD"
)
def parse_amount(value, row_number, field_name):
value = value.strip()
if not value:
return None
cleaned = (
value.replace("$", "")
.replace(",", "")
.strip()
)
# Support accounting-style negatives such as ($12.50).
if cleaned.startswith("(") and cleaned.endswith(")"):
cleaned = "-" + cleaned[1:-1].strip()
try:
amount = Decimal(cleaned)
except InvalidOperation as exc:
raise ValueError(
f"Row {row_number}: invalid {field_name} amount {value!r}"
) from exc
return abs(amount)
```
Then change this line in the loop:
```python
date = datetime.strptime(raw_date, "%m/%d/%Y").date()
```
to:
```python
date = parse_date(row["Transaction Date"], row_number)
```
The rest of the script can stay the same. This accepts values such as `12.50`, `$12.50`, `1,250.00`, and `($12.50)`.
[user]
Could you provide the complete script with those changes included? I want to copy it into a file without having to merge snippets.
[assistant]
Certainly. Save this as `convert_transactions.py` in the same folder as `bank_export.csv`:
```python
import csv
from datetime import datetime
from decimal import Decimal, InvalidOperation
INPUT = "bank_export.csv"
OUTPUT = "budget_import.csv"
OUTPUT_FIELDS = [
"Date", "Payee", "Amount", "Type", "Category", "Notes"
]
def parse_date(value, row_number):
value = value.strip()
for date_format in ("%m/%d/%Y", "%Y-%m-%d"):
try:
return datetime.strptime(value, date_format).date()
except ValueError:
continue
raise ValueError(
f"Row {row_number}: invalid date {value!r}; "
"expected M/D/YYYY or YYYY-MM-DD"
)
def parse_amount(value, row_number, field_name):
value = (value or "").strip()
if not value:
return None
cleaned = value.replace("$", "").replace(",", "").strip()
if cleaned.startswith("(") and cleaned.endswith(")"):
cleaned = "-" + cleaned[1:-1].strip()
try:
amount = Decimal(cleaned)
except InvalidOperation as exc:
raise ValueError(
f"Row {row_number}: invalid {field_name} amount {value!r}"
) from exc
if amount < 0:
amount = abs(amount)
return amount
with open(INPUT, "r", newline="", encoding="utf-8-sig") as source, \
open(OUTPUT, "w", newline="", encoding="utf-8") as target:
reader = csv.DictReader(source)
required = {
"Transaction Date",
"Description",
"Reference",
"Debit",
"Credit",
"Category",
}
actual_fields = set(reader.fieldnames or [])
missing = required - actual_fields
if missing:
raise ValueError(
f"Missing input columns: {sorted(missing)}"
)
writer = csv.DictWriter(
target,
fieldnames=OUTPUT_FIELDS,
extrasaction="ignore",
)
writer.writeheader()
converted_count = 0
for row_number, row in enumerate(reader, start=2):
date = parse_date(row["Transaction Date"], row_number)
debit = parse_amount(
row["Debit"], row_number, "Debit"
)
credit = parse_amount(
row["Credit"], row_number, "Credit"
)
if (debit is None) == (credit is None):
raise ValueError(
f"Row {row_number}: exactly one of Debit or Credit "
"must contain an amount"
)
if debit is not None:
amount = debit
transaction_type = "Expense"
else:
amount = credit
transaction_type = "Income"
writer.writerow({
"Date": date.isoformat(),
"Payee": (row["Description"] or "").strip(),
"Amount": f"{amount:.2f}",
"Type": transaction_type,
"Category": (row["Category"] or "").strip(),
"Notes": (row["Reference"] or "").strip(),
})
converted_count += 1
print(f"Wrote {converted_count} rows to {OUTPUT}")
```
Run it from that folder with:
```text
python convert_transactions.py
```
If your system uses the `python3` command instead, use:
```text
python3 convert_transactions.py
```
A successful run will print something like:
```text
Wrote 400 rows to budget_import.csv
```
The script writes to a new file and does not alter the original export.
[user]
I ran it and got this error:
```text
Traceback (most recent call last):
File "convert_transactions.py", line 67, in <module>
date = parse_date(row["Transaction Date"], row_number)
File "convert_transactions.py", line 19, in parse_date
raise ValueError(
ValueError: Row 14: invalid date '01-15-2026'; expected M/D/YYYY or YYYY-MM-DD
```
Apparently a few rows use hyphens. Can we accept those too?
[assistant]
Yes. Add the hyphenated U.S. format to the list of accepted formats. Replace the `parse_date` function with this one:
```python
def parse_date(value, row_number):
value = value.strip()
accepted_formats = (
"%m/%d/%Y", # 1/15/2026 or 01/15/2026
"%m-%d-%Y", # 1-15-2026 or 01-15-2026
"%Y-%m-%d", # 2026-01-15
)
for date_format in accepted_formats:
try:
return datetime.strptime(value, date_format).date()
except ValueError:
continue
raise ValueError(
f"Row {row_number}: invalid date {value!r}; "
"expected M/D/YYYY, M-D-YYYY, or YYYY-MM-DD"
)
```
Then run:
```text
python convert_transactions.py
```
It will still write every accepted date in the consistent output format, for example:
```text
2026-01-15
```
The error occurred before the output row was written, so rerunning the script is fine. It opens the output file in write mode and recreates it from the beginning.
[user]
Now it says:
```text
ValueError: Row 88: exactly one of Debit or Credit must contain an amount
```
I looked at that row and both columns are blank. It appears to be a separator row. Can the script skip completely blank transaction rows but still reject rows where both fields have amounts?
[assistant]
Yes. Add a check near the start of the loop, before parsing the date. It should skip a row only when all relevant fields are blank. A row with a date or description but no amount will still be rejected.
Replace the beginning of the loop:
```python
for row_number, row in enumerate(reader, start=2):
date = parse_date(row["Transaction Date"], row_number)
```
with:
```python
for row_number, row in enumerate(reader, start=2):
relevant_values = [
row.get("Transaction Date", ""),
row.get("Description", ""),
row.get("Reference", ""),
row.get("Debit", ""),
row.get("Credit", ""),
row.get("Category", ""),
]
if all(not (value or "").strip() for value in relevant_values):
continue
date = parse_date(row["Transaction Date"], row_number)
```
The existing validation remains unchanged:
```python
if (debit is None) == (credit is None):
raise ValueError(
f"Row {row_number}: exactly one of Debit or Credit "
"must contain an amount"
)
```
So the behavior will be:
- Entirely blank transaction row: skipped.
- Both `Debit` and `Credit` blank, but another field populated: rejected.
- Both amounts populated: rejected.
- Exactly one amount populated: converted.
The reported row will therefore be ignored if every listed field is empty.
[user]
Can you show the full script one more time, including the hyphenated dates and blank-row handling? Also, I would like it to print how many rows it skipped.
[assistant]
```python
import csv
from datetime import datetime
from decimal import Decimal, InvalidOperation
INPUT = "bank_export.csv"
OUTPUT = "budget_import.csv"
OUTPUT_FIELDS = [
"Date", "Payee", "Amount", "Type", "Category", "Notes"
]
def parse_date(value, row_number):
value = (value or "").strip()
accepted_formats = (
"%m/%d/%Y",
"%m-%d-%Y",
"%Y-%m-%d",
)
for date_format in accepted_formats:
try:
return datetime.strptime(value, date_format).date()
except ValueError:
continue
raise ValueError(
f"Row {row_number}: invalid date {value!r}; "
"expected M/D/YYYY, M-D-YYYY, or YYYY-MM-DD"
)
def parse_amount(value, row_number, field_name):
value = (value or "").strip()
if not value:
return None
cleaned = value.replace("$", "").replace(",", "").strip()
if cleaned.startswith("(") and cleaned.endswith(")"):
cleaned = "-" + cleaned[1:-1].strip()
try:
amount = Decimal(cleaned)
except InvalidOperation as exc:
raise ValueError(
f"Row {row_number}: invalid {field_name} amount {value!r}"
) from exc
return abs(amount)
with open(INPUT, "r", newline="", encoding="utf-8-sig") as source, \
open(OUTPUT, "w", newline="", encoding="utf-8") as target:
reader = csv.DictReader(source)
required = {
"Transaction Date",
"Description",
"Reference",
"Debit",
"Credit",
"Category",
}
actual_fields = set(reader.fieldnames or [])
missing = required - actual_fields
if missing:
raise ValueError(f"Missing input columns: {sorted(missing)}")
writer = csv.DictWriter(
target,
fieldnames=OUTPUT_FIELDS,
extrasaction="ignore",
)
writer.writeheader()
converted_count = 0
skipped_blank_count = 0
for row_number, row in enumerate(reader, start=2):
relevant_values = [
row.get("Transaction Date", ""),
row.get("Description", ""),
row.get("Reference", ""),
row.get("Debit", ""),
row.get("Credit", ""),
row.get("Category", ""),
]
if all(not (value or "").strip() for value in relevant_values):
skipped_blank_count += 1
continue
date = parse_date(row["Transaction Date"], row_number)
debit = parse_amount(
row["Debit"], row_number, "Debit"
)
credit = parse_amount(
row["Credit"], row_number, "Credit"
)
if (debit is None) == (credit is None):
raise ValueError(
f"Row {row_number}: exactly one of Debit or Credit "
"must contain an amount"
)
if debit is not None:
amount = debit
transaction_type = "Expense"
else:
amount = credit
transaction_type = "Income"
writer.writerow({
"Date": date.isoformat(),
"Payee": (row["Description"] or "").strip(),
"Amount": f"{amount:.2f}",
"Type": transaction_type,
"Category": (row["Category"] or "").strip(),
"Notes": (row["Reference"] or "").strip(),
})
converted_count += 1
print(f"Wrote {converted_count} rows to {OUTPUT}")
print(f"Skipped {skipped_blank_count} completely blank rows")
```
With the separator row included, the output should look like:
```text
Wrote 399 rows to budget_import.csv
Skipped 1 completely blank rows
```
Only rows where all six checked source fields are blank are skipped. Rows containing other data but no valid debit or credit still produce an error.