Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
[user]
I have a CSV export that my budgeting app rejects. Can you help me reformat it?
[assistant]
Sure. Paste a small sample, including the header row and a few representative records. Please replace or remove any sensitive account numbers, descriptions, or other private details first. Also tell me the exact error message, if there is one, and which columns the budgeting app expects.
[user]
Here are the first few lines. The file is called `transactions_export.csv`.
```text
Date,Description,Amount,Category,Account
01/02/2026,Coffee shop,-4.75,Food,Checking
01/03/2026,Paycheck,1250.00,Income,Checking
01/04/2026,Bus pass,-25.00,Transport,Checking
```
The app says: `Invalid date format. Expected YYYY-MM-DD.`
[assistant]
The immediate issue is the date format. Convert the first column from `MM/DD/YYYY` to `YYYY-MM-DD`, while keeping the other columns unchanged. The sample would become:
```text
Date,Description,Amount,Category,Account
2026-01-02,Coffee shop,-4.75,Food,Checking
2026-01-03,Paycheck,1250.00,Income,Checking
2026-01-04,Bus pass,-25.00,Transport,Checking
```
Before changing the whole file, check whether the app requires a different header spelling or a particular sign convention for amounts.
[user]
I can use a shell command. I’m on a Mac, and the file is in my Downloads folder. Is there a simple way to convert just the dates?
[assistant]
Yes. Make a copy first, then use Python so the CSV quoting and commas are handled safely. In Terminal, run:
```bash
cd ~/Downloads
cp transactions_export.csv transactions_export_backup.csv
python3 - <<'PY'
import csv
from datetime import datetime
source = "transactions_export.csv"
target = "transactions_export_dates.csv"
with open(source, newline="", encoding="utf-8-sig") as infile, \
open(target, "w", newline="", encoding="utf-8") as outfile:
reader = csv.DictReader(infile)
fieldnames = reader.fieldnames
if not fieldnames or "Date" not in fieldnames:
raise SystemExit("Could not find a Date column")
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
row["Date"] = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
writer.writerow(row)
print(f"Wrote {target}")
PY
```
This creates `transactions_export_dates.csv` and leaves the original unchanged.
[user]
Before I run it, some descriptions contain commas, like `Grocery store, downtown`. Does that change anything?
[assistant]
No, not with this script. A correctly exported CSV should quote that field, for example:
```text
01/05/2026,"Grocery store, downtown",-38.20,Food,Checking
```
Python’s `csv.DictReader` understands the quoted comma and keeps the description as one field. Avoid using a simple comma-replacement command such as `sed` or `cut` for the entire file, because those can split quoted descriptions incorrectly.
You can inspect a few rows with:
```bash
head -n 6 transactions_export.csv
```
If the comma-containing description is not quoted, the source file is malformed and needs a different cleanup step.
[user]
I checked. The rows with commas are quoted. After converting the dates, the app now says: `Missing required column: Transaction Date`. It apparently does not use `Date`.
[assistant]
Then rename the header from `Date` to `Transaction Date` while writing the converted file. This version preserves all other column names and values:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from datetime import datetime
source = "transactions_export.csv"
target = "transactions_export_ready.csv"
with open(source, newline="", encoding="utf-8-sig") as infile, \
open(target, "w", newline="", encoding="utf-8") as outfile:
reader = csv.DictReader(infile)
if not reader.fieldnames:
raise SystemExit("The file has no header row")
fieldnames = [
"Transaction Date" if name == "Date" else name
for name in reader.fieldnames
]
if "Transaction Date" not in fieldnames:
raise SystemExit("Could not find the Date column")
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
row["Date"] = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
row["Transaction Date"] = row.pop("Date")
writer.writerow(row)
print(f"Wrote {target}")
PY
```
The output header should be:
```text
Transaction Date,Description,Amount,Category,Account
```
Use the new `transactions_export_ready.csv` for the next import attempt.
[user]
Now it reports: `Amount must be a number without currency symbols.` My full export has amounts like `$1,250.00`, and some negative values appear as `($4.75)`. How should those be normalized?
[assistant]
Normalize the `Amount` column to plain decimal numbers: `$1,250.00` becomes `1250.00`, and `($4.75)` becomes `-4.75`. Because commas can also occur inside quoted descriptions, use the CSV parser again. Here is a complete script that handles the date rename and amount cleanup together:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from datetime import datetime
from decimal import Decimal, InvalidOperation
source = "transactions_export.csv"
target = "transactions_export_ready.csv"
def clean_amount(value):
text = value.strip()
if text.startswith("(") and text.endswith(")"):
text = "-" + text[1:-1]
text = text.replace("$", "").replace(",", "").strip()
try:
return format(Decimal(text), "f")
except InvalidOperation:
raise ValueError(f"Invalid amount: {value!r}")
with open(source, newline="", encoding="utf-8-sig") as infile, \
open(target, "w", newline="", encoding="utf-8") as outfile:
reader = csv.DictReader(infile)
if not reader.fieldnames:
raise SystemExit("The file has no header row")
if "Date" not in reader.fieldnames:
raise SystemExit("Could not find Date column")
if "Amount" not in reader.fieldnames:
raise SystemExit("Could not find Amount column")
fieldnames = [
"Transaction Date" if name == "Date" else name
for name in reader.fieldnames
]
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for line_number, row in enumerate(reader, start=2):
try:
row["Date"] = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
row["Amount"] = clean_amount(row["Amount"])
except (ValueError, TypeError) as error:
raise SystemExit(f"Line {line_number}: {error}")
row["Transaction Date"] = row.pop("Date")
writer.writerow(row)
print(f"Wrote {target}")
PY
```
For example, the output values will look like:
```text
1250.00
-4.75
```
No dollar signs, parentheses, or thousands separators remain.
[user]
Some rows have blank categories, and the app says `Category cannot be empty`. I do not want to invent detailed categories. Can I fill only blank ones with `Uncategorized`?
[assistant]
Yes. Add a category fallback that changes only empty or whitespace-only category values. Existing categories remain unchanged. This is the relevant modification:
```python
category = row.get("Category", "")
if not category.strip():
row["Category"] = "Uncategorized"
```
Here is the full script with the date, amount, and category handling included:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from datetime import datetime
from decimal import Decimal, InvalidOperation
source = "transactions_export.csv"
target = "transactions_export_ready.csv"
def clean_amount(value):
text = value.strip()
if text.startswith("(") and text.endswith(")"):
text = "-" + text[1:-1]
text = text.replace("$", "").replace(",", "").strip()
try:
return format(Decimal(text), "f")
except InvalidOperation:
raise ValueError(f"Invalid amount: {value!r}")
with open(source, newline="", encoding="utf-8-sig") as infile, \
open(target, "w", newline="", encoding="utf-8") as outfile:
reader = csv.DictReader(infile)
if not reader.fieldnames:
raise SystemExit("The file has no header row")
required = {"Date", "Amount", "Category"}
missing = required - set(reader.fieldnames)
if missing:
raise SystemExit(f"Missing columns: {', '.join(sorted(missing))}")
fieldnames = [
"Transaction Date" if name == "Date" else name
for name in reader.fieldnames
]
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for line_number, row in enumerate(reader, start=2):
try:
row["Date"] = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
row["Amount"] = clean_amount(row["Amount"])
except (ValueError, TypeError) as error:
raise SystemExit(f"Line {line_number}: {error}")
if not row.get("Category", "").strip():
row["Category"] = "Uncategorized"
row["Transaction Date"] = row.pop("Date")
writer.writerow(row)
print(f"Wrote {target}")
PY
```
The output will retain the original category text where present and use `Uncategorized` only for blank entries.
[user]
The app also requires a column called `Type`, with values `Income` or `Expense`. It says not to use the sign of Amount for this. Can the script derive Type from Category, using `Income` as income and everything else as expense?
[assistant]
Yes. Add a `Type` column based on the cleaned category. Set it to `Income` when the category is exactly `Income`, and `Expense` otherwise. The amount stays numeric and keeps its original sign. Here is the revised script:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from datetime import datetime
from decimal import Decimal, InvalidOperation
source = "transactions_export.csv"
target = "transactions_export_ready.csv"
def clean_amount(value):
text = value.strip()
if text.startswith("(") and text.endswith(")"):
text = "-" + text[1:-1]
text = text.replace("$", "").replace(",", "").strip()
try:
return format(Decimal(text), "f")
except InvalidOperation:
raise ValueError(f"Invalid amount: {value!r}")
with open(source, newline="", encoding="utf-8-sig") as infile, \
open(target, "w", newline="", encoding="utf-8") as outfile:
reader = csv.DictReader(infile)
if not reader.fieldnames:
raise SystemExit("The file has no header row")
required = {"Date", "Amount", "Category"}
missing = required - set(reader.fieldnames)
if missing:
raise SystemExit(f"Missing columns: {', '.join(sorted(missing))}")
fieldnames = [
"Transaction Date" if name == "Date" else name
for name in reader.fieldnames
]
if "Type" not in fieldnames:
fieldnames.append("Type")
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for line_number, row in enumerate(reader, start=2):
try:
row["Date"] = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
row["Amount"] = clean_amount(row["Amount"])
except (ValueError, TypeError) as error:
raise SystemExit(f"Line {line_number}: {error}")
if not row.get("Category", "").strip():
row["Category"] = "Uncategorized"
row["Type"] = (
"Income" if row["Category"].strip() == "Income"
else "Expense"
)
row["Transaction Date"] = row.pop("Date")
writer.writerow(row)
print(f"Wrote {target}")
PY
```
A row with category `Income` receives `Type=Income`; any other category, including `Uncategorized`, receives `Type=Expense`.
[assistant]
I ran it, and it stopped with `Line 842: Invalid amount: ''`. I want to find the problematic row and inspect it without changing the output file.
[assistant]
You can scan the source file and print the CSV line number, parsed fields, and raw amount for blank or malformed amounts. This only reads the file:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from decimal import Decimal, InvalidOperation
source = "transactions_export.csv"
def is_valid_amount(value):
text = value.strip()
if text.startswith("(") and text.endswith(")"):
text = "-" + text[1:-1]
text = text.replace("$", "").replace(",", "").strip()
if not text:
return False
try:
Decimal(text)
return True
except InvalidOperation:
return False
with open(source, newline="", encoding="utf-8-sig") as infile:
reader = csv.DictReader(infile)
for line_number, row in enumerate(reader, start=2):
raw = row.get("Amount", "")
if not is_valid_amount(raw):
print(f"CSV line {line_number}")
print(f" Date: {row.get('Date', '')!r}")
print(f" Description: {row.get('Description', '')!r}")
print(f" Amount: {raw!r}")
print(f" Category: {row.get('Category', '')!r}")
print()
PY
```
The `start=2` accounts for the header row. It reports the logical CSV record number, which is more useful than a shell line count when quoted fields contain embedded line breaks.
[assistant]
The row is a transfer record with a blank amount. It has `Description=Transfer to savings`, `Category=Transfer`, and the account is `Checking`. The app requires an amount, but I do not want to guess a value. What can I do?
[assistant]
Do not invent an amount. The safest options are to correct that row from the original transaction source, remove it from the import, or leave it in a separate review file until the amount is known. You can create a review file containing rows with invalid amounts and a cleaned candidate file containing only valid rows.
This script writes two files and does not assign any replacement amount:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from pathlib import Path
source = "transactions_export.csv"
review = "transactions_export_amount_review.csv"
valid = "transactions_export_valid_amounts.csv"
with open(source, newline="", encoding="utf-8-sig") as infile:
reader = csv.DictReader(infile)
if not reader.fieldnames:
raise SystemExit("The file has no header row")
with open(review, "w", newline="", encoding="utf-8") as review_file, \
open(valid, "w", newline="", encoding="utf-8") as valid_file:
review_writer = csv.DictWriter(
review_file, fieldnames=reader.fieldnames
)
valid_writer = csv.DictWriter(
valid_file, fieldnames=reader.fieldnames
)
review_writer.writeheader()
valid_writer.writeheader()
review_count = valid_count = 0
for row in reader:
text = row.get("Amount", "").strip()
cleaned = text
if cleaned.startswith("(") and cleaned.endswith(")"):
cleaned = "-" + cleaned[1:-1]
cleaned = cleaned.replace("$", "").replace(",", "").strip()
try:
float(cleaned)
has_amount = bool(cleaned)
except ValueError:
has_amount = False
if has_amount:
valid_writer.writerow(row)
valid_count += 1
else:
review_writer.writerow(row)
review_count += 1
print(f"Wrote {valid_count} rows to {valid}")
print(f"Wrote {review_count} rows to {review}")
PY
```
After the missing amount is corrected in a copy of the source, rerun the complete conversion script against that corrected copy.
[assistant]
I found the amount in another export and filled it in as `125.00` for that transfer. The output imports now, but the app displays expenses as positive and income as negative. It expects expenses negative and income positive. Can the script normalize the sign based on `Type`?
[assistant]
Yes. After determining `Type`, convert the amount to an absolute value and apply the required sign: income is positive and expense is negative. This avoids relying on the source sign. Replace the amount cleanup function and the row-processing portion with the following:
```python
def clean_amount(value):
text = value.strip()
if text.startswith("(") and text.endswith(")"):
text = "-" + text[1:-1]
text = text.replace("$", "").replace(",", "").strip()
try:
return Decimal(text)
except InvalidOperation:
raise ValueError(f"Invalid amount: {value!r}")
```
Then, inside the loop:
```python
try:
row["Date"] = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
amount = clean_amount(row["Amount"])
except (ValueError, TypeError) as error:
raise SystemExit(f"Line {line_number}: {error}")
if not row.get("Category", "").strip():
row["Category"] = "Uncategorized"
row["Type"] = (
"Income" if row["Category"].strip() == "Income"
else "Expense"
)
amount = abs(amount)
if row["Type"] == "Expense":
amount = -amount
row["Amount"] = format(amount, "f")
row["Transaction Date"] = row.pop("Date")
writer.writerow(row)
```
Examples:
```text
Source amount Category Type Output amount
1250.00 Income Income 1250.00
-4.75 Food Expense -4.75
4.75 Food Expense -4.75
($4.75) Food Expense -4.75
```
This changes only the sign and keeps the numeric magnitude.
[assistant]
Could you provide one complete final script with all the fixes? I want it to read `transactions_export_corrected.csv`, write `transactions_export_final.csv`, rename the date column, clean amounts, fill blank categories, and add Type.
[assistant]
Use this complete script from `~/Downloads`. It reads the corrected source, leaves it untouched, and writes the final CSV:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
from datetime import datetime
from decimal import Decimal, InvalidOperation
source = "transactions_export_corrected.csv"
target = "transactions_export_final.csv"
def clean_amount(value):
text = (value or "").strip()
if text.startswith("(") and text.endswith(")"):
text = "-" + text[1:-1]
text = text.replace("$", "").replace(",", "").strip()
if not text:
raise ValueError("blank amount")
try:
return Decimal(text)
except InvalidOperation:
raise ValueError(f"invalid amount {value!r}")
with open(source, newline="", encoding="utf-8-sig") as infile, \
open(target, "w", newline="", encoding="utf-8") as outfile:
reader = csv.DictReader(infile)
if not reader.fieldnames:
raise SystemExit("The file has no header row")
required = {"Date", "Amount", "Category"}
missing = required - set(reader.fieldnames)
if missing:
raise SystemExit(
"Missing columns: " + ", ".join(sorted(missing))
)
fieldnames = [
"Transaction Date" if name == "Date" else name
for name in reader.fieldnames
]
if "Type" not in fieldnames:
fieldnames.append("Type")
writer = csv.DictWriter(
outfile,
fieldnames=fieldnames,
extrasaction="raise"
)
writer.writeheader()
count = 0
for line_number, row in enumerate(reader, start=2):
try:
transaction_date = datetime.strptime(
row["Date"].strip(), "%m/%d/%Y"
).strftime("%Y-%m-%d")
amount = abs(clean_amount(row["Amount"]))
except (ValueError, TypeError) as error:
raise SystemExit(f"CSV line {line_number}: {error}")
category = row.get("Category", "").strip()
if not category:
category = "Uncategorized"
row["Category"] = category
transaction_type = (
"Income" if category == "Income" else "Expense"
)
row["Type"] = transaction_type
if transaction_type == "Expense":
amount = -amount
row["Amount"] = format(amount, "f")
row["Transaction Date"] = transaction_date
del row["Date"]
writer.writerow(row)
count += 1
print(f"Wrote {count} rows to {target}")
PY
```
The resulting header should be:
```text
Transaction Date,Description,Amount,Category,Account,Type
```
A quick preview is:
```bash
head -n 6 transactions_export_final.csv
```
You should see ISO dates, plain numeric amounts, nonblank categories, and `Income` or `Expense` in every `Type` field.
[assistant]
The script ran successfully. Before importing, I want a quick check that every row has the expected date format, a numeric amount, a nonblank category, and only the two allowed Type values.
[assistant]
Run this read-only validation command:
```bash
cd ~/Downloads
python3 - <<'PY'
import csv
import re
from decimal import Decimal, InvalidOperation
path = "transactions_export_final.csv"
date_pattern = re.compile(r"^\d{4}-\d{2}-\d{2}$")
allowed_types = {"Income", "Expense"}
errors = []
row_count = 0
with open(path, newline="", encoding="utf-8-sig") as infile:
reader = csv.DictReader(infile)
expected = {
"Transaction Date",
"Description",
"Amount",
"Category",
"Account",
"Type",
}
actual = set(reader.fieldnames or [])
missing = expected - actual
if missing:
errors.append(
"Missing columns: " + ", ".join(sorted(missing))
)
for line_number, row in enumerate(reader, start=2):
row_count += 1
date = row.get("Transaction Date", "")
amount = row.get("Amount", "")
category = row.get("Category", "")
transaction_type = row.get("Type", "")
if not date_pattern.fullmatch(date):
errors.append(
f"line {line_number}: bad date {date!r}"
)
try:
Decimal(amount)
except (InvalidOperation, TypeError):
errors.append(
f"line {line_number}: bad amount {amount!r}"
)
if not category.strip():
errors.append(f"line {line_number}: blank category")
if transaction_type not in allowed_types:
errors.append(
f"line {line_number}: bad Type {transaction_type!r}"
)
if errors:
print("Validation failed:")
print("\n".join(errors[:50]))
if len(errors) > 50:
print(f"... and {len(errors) - 50} more")
raise SystemExit(1)
print(f"Validation passed: {row_count} data rows checked")
PY
```
A successful result will look like:
```text
Validation passed: 842 data rows checked
```
If it reports errors, it will include the CSV line number and the specific field value that failed.
[user]
The validation passed. The preview has quoted descriptions with commas and the dates look right. Is there anything simple I should check before importing?
[assistant]
Do a final visual check of the header, a few rows from different categories, and the file size. These commands do not modify the file:
```bash
cd ~/Downloads
printf '%s\n' '--- header and first rows ---'
head -n 6 transactions_export_final.csv
printf '%s\n' '--- last rows ---'
tail -n 5 transactions_export_final.csv
printf '%s\n' '--- file details ---'
wc -l -c transactions_export_final.csv
ls -lh transactions_export_final.csv
```
Check that:
- The header says `Transaction Date`, not `Date`.
- Dates use `YYYY-MM-DD`.
- Amounts have no `$`, commas, or parentheses.
- Expense amounts are negative and income amounts are positive.
- Categories are populated.
- `Type` contains only `Income` or `Expense`.
- Descriptions containing commas remain enclosed in quotes.
- The row count is plausible compared with the corrected source.
Keep `transactions_export_corrected.csv` and the final output available until the import has completed. If the app offers a preview, inspect a few rows there before confirming the import.