pythonpython-3.xtoml

how to write to a file with a fixed template in python?


I have a fixed template to be written which is pretty long as,

REQUEST DETAILS
RITM :: RITM1234
STASK :: TASK1234
EMAIL :: abc@abc.com
USER :: JOHN JOY

CONTENT DETAILS
TASK STATE :: OPEN
RAISED ON :: 12-JAN-2021
CHANGES :: REMOVE LOG

something like this, which would be 100 lines.

Do we have any way to store it as template or store it in files like ".toml" or similar files and write to values(right side of ::) in python?


Solution

  • Put all the inputs as placeholders using $ and save it as txt file. Then use Template

    from string import Template
    t = Template(open('template.txt', 'r'))
    t.substitute(params_dict)
    

    Sample,

    >>> from string import Template
    >>> t = Template('Hey, $name!')
    >>> t.substitute(name='Bob')
    'Hey, Bob!'