You can do this with autobean-refactor 
<https://github.com/SEIAROTg/autobean-refactor>.

Note that if you did it with Beancount as it currently us, like with the 
code you posted, it would be writing out processed entries. I.e., entries 
that have been through interpolation, plugins, and such. In most cases, 
that is not what you want.
​

On Friday, March 15, 2024 at 3:47:56 PM UTC-7 Saglara S 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 beancount+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beancount/6b89c45f-bcd2-489c-956b-46e3aab2f0een%40googlegroups.com.

Reply via email to