Skip to content

Instantly share code, notes, and snippets.

@amelandri
Last active February 17, 2024 11:19
Show Gist options
  • Save amelandri/054c67c4585c00683f086b816b45af1a to your computer and use it in GitHub Desktop.
Save amelandri/054c67c4585c00683f086b816b45af1a to your computer and use it in GitHub Desktop.
hledger journal cleanup script
inputFile = 'input.journal'
outputFile = 'output.journal'
alignColumn = 100
decimalSeparator = ','
addMissingDecimals = True
with open(inputFile, 'r') as file_input, open(outputFile, 'w') as file_output:
for line in file_input:
if line.strip().startswith(("assets:", "expenses:", "revenues:", "liabilities:")):
account, amount_comment = line.rsplit(' ', 1)
amount_comment = amount_comment.strip();
comment = ''
if ';' not in amount_comment:
amount = amount_comment.strip()
else:
amount_comment_split = amount_comment.split(';')
amount = amount_comment_split[0].strip()
comment = ' ; ' + amount_comment_split[1].strip()
if addMissingDecimals and amount != '' and decimalSeparator not in amount:
amount += decimalSeparator + '00'
spaces = alignColumn - 1 - len(account) - len(amount)
newline = f"{account}{' ' * spaces}{amount}{comment}\n"
file_output.write(newline)
else:
file_output.write(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment