Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript
13.3 KB
Raw Blame
[user]
I have a CSV export from a spreadsheet that my budgeting app rejects. Can you help me reformat it?

[assistant]
Sure. Paste a small sample of the CSV, including the header row, and tell me what the budgeting app expects if you know it. Please remove or replace any sensitive account details first.

[user]
Here are the first few rows. The file uses semicolons because of my spreadsheet settings:

Date;Description;Category;Amount;Account
03/01/2026;Grocery store;Food;42.18;Checking
03/02/2026;Bus pass;Transport;25.00;Checking
03/03/2026;Paycheck;Income;1850.00;Checking
03/04/2026;Coffee;Food;4.75;Cash

The app says it needs Date, Payee, Amount, and Account, with commas between fields.

[assistant]
That is straightforward. The main changes are:

- Replace semicolons with commas.
- Rename Description to Payee.
- Remove Category, unless the app has an optional category column.
- Keep the amount as a plain number with a decimal point.
- Use a date format the app accepts, preferably `2026-03-01` unless its instructions specify another format.

The converted sample would be:

Date,Payee,Amount,Account
2026-03-01,Grocery store,42.18,Checking
2026-03-02,Bus pass,25.00,Checking
2026-03-03,Paycheck,1850.00,Checking
2026-03-04,Coffee,4.75,Cash

[assistant]
Can I do this with a command-line tool instead of editing the whole file manually? I’m on macOS, and the source file is called `march-export.csv`.

[assistant]
Yes. If the fields do not contain semicolons, commas, or line breaks inside quoted values, this quick command will work:

```sh
awk -F';' 'BEGIN { OFS="," }
NR == 1 { print "Date,Payee,Amount,Account"; next }
{ print $1, $2, $4, $5 }
' march-export.csv > march-budget.csv
```

Then inspect the result:

```sh
sed -n '1,8p' march-budget.csv
```

Expected output:

```text
Date,Payee,Amount,Account
03/01/2026,Grocery store,42.18,Checking
03/02/2026,Bus pass,25.00,Checking
03/03/2026,Paycheck,1850.00,Checking
03/04/2026,Coffee,4.75,Cash
```

This changes the delimiter and columns, but it does not convert the dates.

[assistant]
I ran it, and the output looks okay except the app rejects the dates. It specifically requires `YYYY-MM-DD`. Also, some descriptions have commas, such as `Lunch, takeaway`.

[assistant]
Use a CSV-aware parser rather than `awk`, because a description containing a comma needs quoting. Python’s standard library can read the semicolon-delimited source and write valid comma-delimited CSV.

Create a file named `convert_budget.py`:

```python
import csv
from datetime import datetime

source = "march-export.csv"
destination = "march-budget.csv"

with open(source, "r", newline="", encoding="utf-8-sig") as infile, \
     open(destination, "w", newline="", encoding="utf-8") as outfile:

    reader = csv.DictReader(infile, delimiter=";")
    writer = csv.DictWriter(
        outfile,
        fieldnames=["Date", "Payee", "Amount", "Account"],
        lineterminator="\n"
    )

    writer.writeheader()

    for row in reader:
        original_date = row["Date"].strip()
        converted_date = datetime.strptime(
            original_date, "%m/%d/%Y"
        ).strftime("%Y-%m-%d")

        amount = row["Amount"].strip().replace(",", ".")

        writer.writerow({
            "Date": converted_date,
            "Payee": row["Description"].strip(),
            "Amount": amount,
            "Account": row["Account"].strip(),
        })
```

Run it with:

```sh
python3 convert_budget.py
```

The CSV module will automatically quote a payee like `Lunch, takeaway`.

[assistant]
I ran that and got this error:

```text
Traceback (most recent call last):
  File "convert_budget.py", line 22, in <module>
    original_date = row["Date"].strip()
AttributeError: 'NoneType' object has no attribute 'strip'
```

[assistant]
That usually means at least one row has a blank or malformed date field. First, identify rows whose field count is not five:

```sh
python3 - <<'PY'
import csv

with open("march-export.csv", newline="", encoding="utf-8-sig") as f:
    for line_number, row in enumerate(csv.reader(f, delimiter=";"), start=1):
        if len(row) != 5:
            print(f"line {line_number}: {len(row)} fields: {row}")
PY
```

Also check for blank dates:

```sh
python3 - <<'PY'
import csv

with open("march-export.csv", newline="", encoding="utf-8-sig") as f:
    for line_number, row in enumerate(csv.DictReader(f, delimiter=";"), start=2):
        if not row.get("Date", "").strip():
            print(f"line {line_number}: blank date: {row}")
PY
```

If the problem is a trailing blank line, the converter can skip it. If there is a genuinely blank date, you need to decide whether to fill it from the source or omit that transaction. For a routine import, omitting incomplete rows is safer than inventing a date.

[assistant]
The first check printed this:

```text
line 9: 6 fields: ['03/08/2026', 'Lunch, takeaway', 'Food', '12.40', 'Checking', '']
line 17: 6 fields: ['03/16/2026', 'Hardware; small item', 'Household', '8.99', 'Cash', '']
```

The source has quotation marks around the comma example, but apparently not around the semicolon example. The extra field is an empty trailing field.

[assistant]
The comma example is valid if it is quoted in the source, but an unquoted semicolon inside a semicolon-delimited row cannot be distinguished from a separator. Since the extra field is empty, you can safely remove only that trailing field and keep the first five fields. The semicolon in the description will still be split, though, so the second row’s payee would be truncated unless you repair that source row.

A tolerant converter can handle the empty trailing field and report rows with other structural problems:

```python
import csv
from datetime import datetime

source = "march-export.csv"
destination = "march-budget.csv"

with open(source, "r", newline="", encoding="utf-8-sig") as infile, \
     open(destination, "w", newline="", encoding="utf-8") as outfile:

    reader = csv.reader(infile, delimiter=";")
    writer = csv.DictWriter(
        outfile,
        fieldnames=["Date", "Payee", "Amount", "Account"],
        lineterminator="\n"
    )
    writer.writeheader()

    header = next(reader)

    for line_number, fields in enumerate(reader, start=2):
        if not any(field.strip() for field in fields):
            continue

        if len(fields) == 6 and fields[-1].strip() == "":
            fields = fields[:-1]

        if len(fields) != 5:
            print(f"Skipped line {line_number}: expected 5 fields, got {len(fields)}")
            continue

        date_text, payee, category, amount, account = fields

        try:
            date_text = datetime.strptime(
                date_text.strip(), "%m/%d/%Y"
            ).strftime("%Y-%m-%d")
        except ValueError:
            print(f"Skipped line {line_number}: invalid date {date_text!r}")
            continue

        writer.writerow({
            "Date": date_text,
            "Payee": payee.strip(),
            "Amount": amount.strip().replace(",", "."),
            "Account": account.strip(),
        })
```

For the unquoted semicolon row, edit the source to either remove that semicolon or quote the whole description field, for example:

```text
03/16/2026;"Hardware; small item";Household;8.99;Cash
```

Then run the script again.

[assistant]
I repaired that row and reran the script. It created the file. How can I verify that the output is valid before importing it?

[assistant]
You can check the header, row structure, dates, amounts, and whether Python can read the output as CSV:

```sh
python3 - <<'PY'
import csv
from datetime import datetime
from decimal import Decimal

path = "march-budget.csv"
expected = ["Date", "Payee", "Amount", "Account"]
count = 0
errors = []

with open(path, newline="", encoding="utf-8") as f:
    reader = csv.DictReader(f)

    if reader.fieldnames != expected:
        errors.append(f"header is {reader.fieldnames!r}, expected {expected!r}")

    for line_number, row in enumerate(reader, start=2):
        count += 1

        try:
            datetime.strptime(row["Date"], "%Y-%m-%d")
        except (TypeError, ValueError):
            errors.append(f"line {line_number}: invalid date {row['Date']!r}")

        try:
            Decimal(row["Amount"])
        except Exception:
            errors.append(f"line {line_number}: invalid amount {row['Amount']!r}")

        if not row["Payee"].strip():
            errors.append(f"line {line_number}: blank payee")

        if not row["Account"].strip():
            errors.append(f"line {line_number}: blank account")

print(f"Rows checked: {count}")
if errors:
    print("Problems:")
    print("\n".join(errors))
    raise SystemExit(1)
else:
    print("No validation problems found.")
PY
```

You can also inspect quoted fields directly:

```sh
sed -n '1,12p' march-budget.csv
```

A payee containing a comma should look similar to:

```text
2026-03-08,"Lunch, takeaway",12.40,Checking
```

A payee containing a semicolon usually does not need quotes in the output, but it is fine if the CSV writer includes them.

[assistant]
The validation passed. The app has one more requirement: expenses must be negative, while income must be positive. In the source, all amounts are positive, and the Category column identifies income.

[assistant]
Add the category back into the conversion logic only as a way to determine the sign, while keeping the output columns unchanged. Replace the loop in the script with this version:

```python
    for line_number, fields in enumerate(reader, start=2):
        if not any(field.strip() for field in fields):
            continue

        if len(fields) == 6 and fields[-1].strip() == "":
            fields = fields[:-1]

        if len(fields) != 5:
            print(f"Skipped line {line_number}: expected 5 fields, got {len(fields)}")
            continue

        date_text, payee, category, amount, account = fields

        try:
            date_text = datetime.strptime(
                date_text.strip(), "%m/%d/%Y"
            ).strftime("%Y-%m-%d")
        except ValueError:
            print(f"Skipped line {line_number}: invalid date {date_text!r}")
            continue

        try:
            value = float(amount.strip().replace(",", "."))
        except ValueError:
            print(f"Skipped line {line_number}: invalid amount {amount!r}")
            continue

        if category.strip().lower() == "income":
            value = abs(value)
        else:
            value = -abs(value)

        writer.writerow({
            "Date": date_text,
            "Payee": payee.strip(),
            "Amount": f"{value:.2f}",
            "Account": account.strip(),
        })
```

After rerunning:

```sh
python3 convert_budget.py
sed -n '1,8p' march-budget.csv
```

You should see output shaped like:

```text
Date,Payee,Amount,Account
2026-03-01,Grocery store,-42.18,Checking
2026-03-02,Bus pass,-25.00,Checking
2026-03-03,Paycheck,1850.00,Checking
2026-03-04,Coffee,-4.75,Cash
```

This treats only rows whose category is exactly `Income`, ignoring capitalization and surrounding spaces, as positive.

[assistant]
That worked. Could you give me a final complete script so I do not have to assemble the pieces?

[assistant]
Here is the complete script:

```python
import csv
from datetime import datetime

SOURCE = "march-export.csv"
DESTINATION = "march-budget.csv"

with open(SOURCE, "r", newline="", encoding="utf-8-sig") as infile, \
     open(DESTINATION, "w", newline="", encoding="utf-8") as outfile:

    reader = csv.reader(infile, delimiter=";")
    writer = csv.DictWriter(
        outfile,
        fieldnames=["Date", "Payee", "Amount", "Account"],
        lineterminator="\n"
    )

    header = next(reader, None)
    if header is None:
        raise SystemExit("The source file is empty.")

    writer.writeheader()

    for line_number, fields in enumerate(reader, start=2):
        if not any(field.strip() for field in fields):
            continue

        # Remove an empty extra field sometimes added at the end of a row.
        if len(fields) == 6 and fields[-1].strip() == "":
            fields = fields[:-1]

        if len(fields) != 5:
            print(
                f"Skipped line {line_number}: "
                f"expected 5 fields, got {len(fields)}"
            )
            continue

        date_text, payee, category, amount_text, account = fields

        try:
            output_date = datetime.strptime(
                date_text.strip(), "%m/%d/%Y"
            ).strftime("%Y-%m-%d")
        except ValueError:
            print(f"Skipped line {line_number}: invalid date {date_text!r}")
            continue

        try:
            amount = float(amount_text.strip().replace(",", "."))
        except ValueError:
            print(
                f"Skipped line {line_number}: "
                f"invalid amount {amount_text!r}"
            )
            continue

        if category.strip().lower() == "income":
            amount = abs(amount)
        else:
            amount = -abs(amount)

        writer.writerow({
            "Date": output_date,
            "Payee": payee.strip(),
            "Amount": f"{amount:.2f}",
            "Account": account.strip(),
        })

print(f"Wrote {DESTINATION}")
```

Save it as `convert_budget.py`, then run:

```sh
python3 convert_budget.py
```

For a final quick check:

```sh
head -n 10 march-budget.csv
```

The output should have exactly these columns:

```text
Date,Payee,Amount,Account
```

and dates such as `2026-03-08`, with expenses negative and income positive.