0

I have the following a markdown file "loan.md":

#### <u>Loan data:</u>
+ Loan amount: {amount}
+ Loan term: {term}
+ Annual istallments: {instalments}
+ Loan interest rate: {rate}

#### <u>Monthly loan installment:</u>
+ Monthly payment: {payment}

#### <u>Formula to calculate the monthly loan installment:</u>

\[
P = A \times\frac{r_i}{1-(1+r_i)^{-t}}
\]


<u>Where:</u>


\[
r_k = \frac{r}{i}
\]

+ $P$ : Monthly payment.
+ $A$ : Loan amount.
+ $r$ : Interest rate.
+ $i$ :Annual istallments.
+ $t$ : Loan term.

I have the following script in python

loan_data = {'amount':'10000.00',
             'term':'60',
             'installments':'12',
             'rate':'5,95%',
             'payment':'193,10'
             }

with open('loan.md') as f:
    text = f.read()
    text = text.format(**loan_data)
    out_file = open('output.md', 'w')
    out_file.write(text)

When I run the script a get an error the following error message:

Traceback (most recent call last):
  File "loan.py", line 10, in <module>
    text = text.format(**loan_data)
KeyError: 'r_i'

I know that the error is produced by the Latex in the markdown

It is possible to load a markdown file like the one above in python and fill the replacement field with data?

McNulty
  • 57
  • 1
  • 6
  • 1
    The document is Markdown, not a valid Python format string. It is not impossible to write some code to convert it but that's not what you are asking, is it? Probably figure out a way to represent curly braces intended for the Python format string differently from verbatim ones. Trivially, you can double the ones which should be passed through verbatim. – tripleee Jan 22 '20 at 17:52
  • that solve my problem. Thanks very much, tripleee!!! – McNulty Jan 22 '20 at 18:13

0 Answers0