There's a far fetched idea for the next version to augment the parser such
that it is possible to completely reverse the process and regenerate the
input file with modifications, but it's just a fanciful idea at this point.


On Fri, Mar 15, 2024 at 6:47 PM Saglara S <[email protected]> wrote:

> I want to add and modify postings in transactions based on a lookup table
> (search string : account). The script here does the job and will print out
> the modified transactions.
>
> But how on earth to modify / update the transactions in the input file
> accordingly?
>
> import json
> import re
> from beancount import loader
> from beancount.core import data
> from beancount.parser import printer
>
> # Define the filename and load the Beancount file
> filename = "comdirect-mitko.beancount"
> entries, errors, options = loader.load_file(filename)
>
> # Define the JSON lookup table
> lookup_table = {
>     "AGIP": "Assets:Test",
>     "PayPal Europe": "Assets:Transfer:Paypal",
>     # Add more entries as needed
> }
>
> # Loop over each key-value pair in the lookup table
> for search_string, posting_account in lookup_table.items():
>     for entry in entries:
>         if isinstance(entry, data.Transaction):
>             if re.search(search_string, (entry.payee or entry.narration)):
>                 posting = data.Posting(
>                     posting_account,
>                     None,
>                     None,
>                     None,
>                     None,
>                     None,
>                 )
>                 if len(entry.postings) == 1:
>                     entry.postings.append(posting)
>                 elif len(entry.postings) == 2:
>                     entry.postings[1] = posting
>                 printer.print_entry(entry)  # Print the modified entry
>
> --
> You received this message because you are subscribed to the Google Groups
> "Beancount" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/beancount/11a9fd9a-f6b4-42d6-9851-8c9ef21c71acn%40googlegroups.com
> <https://groups.google.com/d/msgid/beancount/11a9fd9a-f6b4-42d6-9851-8c9ef21c71acn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Beancount" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beancount/CAK21%2BhN_bfEsBK4VnVhgG9cNLk_pJfeU3cTETnEpAV-gB3U8uw%40mail.gmail.com.

Reply via email to