replace(infile, outfile, tokens, values, formats=None, encoding='utf-8')

Replace values in a file using delimiters.

infile : str

Name of template file that will be parsed.

outfile : str

Name of file that will received the template parsed. If equal to None or to infile, the file will be replaced in-place.

tokens : list of str

Regexes that will be replaced. When dealing with overlapping tokens, longest tokens should go first.

values : list

Values (can be string, float, …) that will replace the tokens. The list must have the same size as tokens.

formats : list of str, optional

A list of formats for printing the values in the parsed files at tokens locations see

encoding : str, optional

File encoding see


parameters badly set


a token has not been found


>>> import openturns.coupling_tools as otct
>>> # write a template file
>>> with open('', 'w') as f:
...     count = f.write('E=@E_VAR F=-PF-')
>>> # replace tokens from template
>>> otct.replace('', 'prgm.dat',
...     tokens=["@E_VAR", '-PF-'], values=[1.4, '5'])
>>> # display file
>>> with open('prgm.dat', 'r') as f:
...     print(
E=1.4 F=5