2014-11-05 08:18:13 +03:00
|
|
|
|
# How to read CSV files
|
|
|
|
|
|
2016-07-20 03:25:18 +03:00
|
|
|
|
Here's a quick example of [converting a CSV file](manual.html#csv-format).
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
|
|
|
|
Say we have downloaded `checking.csv` from a bank for the first time:
|
2015-05-26 03:56:00 +03:00
|
|
|
|
```csv
|
|
|
|
|
"Date","Note","Amount"
|
|
|
|
|
"2012/3/22","DEPOSIT","50.00"
|
|
|
|
|
"2012/3/23","TRANSFER TO SAVINGS","-10.00"
|
|
|
|
|
```
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
2016-07-20 03:25:18 +03:00
|
|
|
|
We tell hledger how to intepret this with a file named `checking.csv.rules`, using the [CSV rules syntax](manual.html#csv-format). Eg:
|
2015-05-26 03:56:00 +03:00
|
|
|
|
```rules
|
|
|
|
|
# skip the first CSV line (headings)
|
|
|
|
|
skip 1
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
2015-05-26 03:56:00 +03:00
|
|
|
|
# use the first three fields in each CSV record as transaction date, description and amount respectively
|
|
|
|
|
fields date, description, amount
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
2015-05-26 03:56:00 +03:00
|
|
|
|
# prepend $ to CSV amounts
|
|
|
|
|
currency $
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
2015-05-26 03:56:00 +03:00
|
|
|
|
# always set the first account to assets:bank:checking
|
|
|
|
|
account1 assets:bank:checking
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
2015-05-26 03:56:00 +03:00
|
|
|
|
# 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)
|
|
|
|
|
if ~ SAVINGS
|
|
|
|
|
account2 assets:bank:savings
|
|
|
|
|
```
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
|
|
|
|
Now hledger can read this CSV file as journal data:
|
|
|
|
|
|
2015-05-26 03:56:00 +03:00
|
|
|
|
```shell
|
|
|
|
|
$ 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
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
2015-05-26 03:56:00 +03:00
|
|
|
|
2012/03/23 TRANSFER TO SAVINGS
|
|
|
|
|
assets:bank:savings $10.00
|
|
|
|
|
assets:bank:checking $-10.00
|
|
|
|
|
```
|
2014-11-05 08:18:13 +03:00
|
|
|
|
|
|
|
|
|
We might save this output as `checking.journal`, and/or merge it (manually) into the main journal file.
|
|
|
|
|
We could also run other commands:
|
2015-05-26 03:56:00 +03:00
|
|
|
|
```shell
|
|
|
|
|
$ 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
|
|
|
|
|
--------------------
|
|
|
|
|
0
|
|
|
|
|
```
|