Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompting script to replace placeholders in template file

Tags:

python

How can I write a Python script that takes a plain text file littered with placeholders, and then prompts me to replace each placeholder before sending it out to a provided email address?

Essentially, I’m looking to use something similar to 'Replacing text with variables' but I’d like to input the different variables from the terminal like in a simple bash script (rather than defining them in the script). I’d like to begin scripting in Python rather than continuing to write bash scripts; it’s for the best.

like image 252
Kevin Steinhardt Avatar asked Oct 24 '25 17:10

Kevin Steinhardt


1 Answers

Once you have the template with the placeholders, you just need to render it using a dictionary that contains all the variables you need in the template as context. This is the approach used by most of the templating systems.

For example,

value = raw_input('prompt> ')

template = 'This is the {value}'
context = {'value': value}
print template.format(**context)
like image 57
jcollado Avatar answered Oct 26 '25 07:10

jcollado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!