Convert CSV files

Simon Michael edited this page Feb 10, 2019 · 8 revisions

Here's a quick example of reading CSV data with hledger.

Say we have downloaded checking.csv from a bank for the first time:

"2012/3/23","TRANSFER TO SAVINGS","-10.00"

We tell hledger how to intepret this with a file named checking.csv.rules, using the CSV rules syntax. Eg:

# skip the first CSV line (headings)
skip 1

# use the first three fields in each CSV record as transaction date, description and amount respectively
fields   date, description, amount

# prepend $ to CSV amounts
currency $

# always set the first account to assets:bank:checking
account1 assets:bank:checking

# if the CSV record contains ‘SAVINGS’, set the second account to assets:bank:savings
# (if not set, it will be expenses:unknown or income:unknown)
  account2 assets:bank:savings

Now hledger can read this CSV file as journal data:

$ hledger -f checking.csv print
using conversion rules file checking.csv.rules
2012/03/22 DEPOSIT
    income:unknown             $-50.00
    assets:bank:checking        $50.00

    assets:bank:savings         $10.00
    assets:bank:checking       $-10.00

We might save this output as checking.journal, and/or merge it (manually, or using the import command) into the main journal file.

We could also just run reports on the CSV file directly:

$ hledger -f checking.csv balance
using conversion rules file checking.csv.rules
              $50.00  assets:bank
              $40.00    checking
              $10.00    savings
             $-50.00  income:unknown

Here are more CSV rules examples.

Here's how to Customize default CSV accounts.

There are many alternate CSV conversion tools at -> data import/conversion (nine CSV->*ledger tools at last count). hledger-import-dsl is a fully programmable hledger-ish option.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.