replace

replace(infile, outfile, tokens, values, formats=None, encoding='ascii')

Replace values in a file using delimiters.

Parameters:

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 https://docs.python.org/2/library/string.html#formatspec

encoding : str, optional

Raises:

AssertionError

parameters badly set

EOFError

a token has not been found

Examples

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