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)