Search code examples
pythonstring-interpolation

What is the most efficient way to read from a template text file and then write interpolated values from different variables from a script?


I have a Python script that I want to output with a README.txt files for users. In the README.txt, there are going to be some description of what was run in the script, including the values. The values of these variables change depending on the project. Since the README.txt file is kind of long, and copying and pasting values to multiple README.txt files is time consuming, I wanted to create and quick and easy function that would read in a template README.txt file and then interpolate the correct values in their appropriate places, creating a new, pertinent README.txt file.

So an example snippet from the README.txt template would read as:

In this script, we ran the statistical processes with variable A set to {variableA} and variable B set to {variableB}. 

We also used the following comparisons where we examined group {group1} vs group {group2}.

I have placed the curly brackets to indicate where I want variables from my script (i.e. variableA, variableB, group1, and group2) so their values are interpolated into the correct places. But I am not certain how best I should format my README.txt template and set it up to efficiently take in the values of the relevant variables. All these variables are defined globally, so scope should not be a problem here.

So far my code reads:

with open("README_template.txt") as readme:
    readme = readme.readlines()
    readme_new = []
    for line in readme:
          #interpolate the pertinent values into the line if necessary
          readme_new.append(line)
    #write out new README.txt file

Solution

  • template.txt

    In this script, we ran the statistical processes with variable A set to {variableA} and variable B set to {variableB}. 
    We also used the following comparisons where we examined group {group1} vs group {group2}.
    

    your script:

    data = {'variableA':'A', 'variableB':'B',
            'group1':1, 'group2':2}
    
    with open('template.txt', 'r') as f, open('README.txt', 'w') as fout:
        templ = f.read()
        fout.write(templ.format(**data))
    

    resulting README.txt

    In this script, we ran the statistical processes with variable A set to A and variable B set to B. 
    We also used the following comparisons where we examined group 1 vs group 2.
    

    Now, if you change your template.txt to

    In this script, we ran the statistical processes with variable A set to {{variableA}} and variable B set to {{variableB}}. 
    We also used the following comparisons where we examined group {{group1}} vs group {{group2}}.
    

    and your script to:

    import jinja2
    
    data = {'variableA':'A', 'variableB':'B',
            'group1':1, 'group2':2}
    
    tmpl_loader = jinja2.FileSystemLoader(searchpath='.')
    tmpl_env = jinja2.Environment(loader=tmpl_loader)
    template = tmpl_env.get_template('template.txt')
    with open('README1.txt', 'w') as fout:
        fout.write(template.render(**data))
    

    you can get the same. HOWEVER, jinja2, as template engine provides powerful tools to generate the output file (e.g. loops, conditionals, filters, etc.) in comparisons with simple string formatting .format()