I think I probably misunderstand the question. Why is it not possible just to print entries in the new file?
printer.print_entries(entries, file=open("new_file.bean", "w", encoding=" utf-8")) On Friday, March 15, 2024 at 11:47:56 PM UTC+1 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/46e6f57f-bcf4-480c-ae2a-8561754ff437n%40googlegroups.com.