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/11a9fd9a-f6b4-42d6-9851-8c9ef21c71acn%40googlegroups.com.

Reply via email to