1

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

REQUEST DETAILS
RITM :: RITM1234
STASK :: TASK1234
EMAIL :: [email protected]
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?

1
  • 3
    You should try string.Template. template string can be stored in a simple text file and can be written into file format of our choice after substitution. Commented Aug 18, 2021 at 12:23

2 Answers 2

4

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!'
Sign up to request clarification or add additional context in comments.

Comments

2

For template creation I use jinja :

from jinja2 import FileSystemLoader, Template

# Function creating from template files.
def write_file_from_template(template_path, output_name, template_variables, output_directory):
    template_read = open(template_path).read()
    template = Template(template_read)
    rendered = template.render(template_variables)
    output_path = os.path.join(output_directory, output_name)
    output_file = open(output_path, 'w+')
    output_file.write(rendered)
    output_file.close()
    print('Created file at  %s' % output_path)
    return output_path



journal_output = write_file_from_template(
        template_path=template_path,
        output_name=output_name,
        template_variables={'file_output':file_output, 
            'step_size':step_size, 
            'time_steps':time_steps},
        output_directory=output_directory)

With a file named file.extension.TEMPLATE:

# This is a new file :
{{ file_output }}
# The step size is :
{{ step_size }}
# The time steps are :
{{ time_steps }}

You may need to modify it a little bit, but the major things are there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.