lossless ledger journal parser

160 views
Skip to first unread message

Ryan Nowakowski

unread,
Sep 5, 2016, 2:53:18 PM9/5/16
to Ledger
Hey Ledger Folks,

I'm building a tool for automatically categorizing transactions based
on regex matching certain journal fields. It's a standard UNIX filter
that takes a journal file from stdin, modifies it and sends it to stdout.
I want to maintain the exact format of the journal including comments.
I haven't figured out a way to do this with the ledger parser python lib
so I wrote my own[1]. Does anyone else have a better way to do this?
I'd rather not maintain my own parser.

Thanks!

Ryan

[1] https://gist.github.com/tubaman/0e80cec388d3d5d61e3f300e2477a9ae

Manuel Amador (Rudd-O)

unread,
Oct 8, 2016, 1:56:41 PM10/8/16
to ledge...@googlegroups.com
I have a robust parser for ledger files in
https://github.com/Rudd-O/ledgerhelpers , but it is missing some
features. It is meant to preserve the exact format and comments in the
file, so nothing gets destroyed in the parsing process.

I will welcome pull requests that add those features.

--
Rudd-O
http://rudd-o.com/

Ryan Nowakowski

unread,
Oct 12, 2016, 2:49:21 PM10/12/16
to ledge...@googlegroups.com
What do you think about trying to add a mode the C++ ledger parser to
do this? I think that'a better long-term option because:

1. It's the real parser so it handles all the little edge cases
2. We don't have to maintain a separate parser

- Ryan

Manuel Amador (Rudd-O)

unread,
Dec 5, 2016, 7:11:41 AM12/5/16
to ledge...@googlegroups.com
I don't know C++, but if someone did add such a mode to Ledger, I'd
probably use it myself.


--
Rudd-O
http://rudd-o.com/

Fergus Cameron

unread,
Dec 5, 2016, 2:18:11 PM12/5/16
to ledge...@googlegroups.com
Additional though: I have also thought about adding a similar mode
which expands the periodic transactions (thus removing the permanent
dependency on generated transactions).
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Ledger" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ledger-cli+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

Ryan Nowakowski

unread,
May 28, 2021, 7:03:00 PMMay 28
to Ledger
Update: I'm now using ledger's built-in parser to losslessly edit ledger
journal files. I've updated the gist[1]. You can use it like this:

from ledgerparser import parse_xacts

xacts = parse_xacts(ledger_file)
for lines, xact in xacts:
if xact...: # this is the xact you're looking for
for line in lines:
# edit the line however you want using re.sub or whatever
sys.stdout.write(line)
Reply all
Reply to author
Forward
0 new messages