replace

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

Replace values in a file using delimiters.

Parameters:
infilestr

Name of template file that will be parsed.

outfilestr

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

tokenslist of str

Regexes that will be replaced. Look out for overlapping tokens (eg @x1 and @x10): a solution can be to use padding (@x001, @x010) or use markers at both sides (@x1@, @x10@), else longer tokens should be listed first.

valueslist

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

formatslist 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

encodingstr, optional

File encoding see http://docs.python.org/2/library/codecs.html#codec-base-classes

Raises:
AssertionError

parameters badly set

EOFError

a token has not been found

Examples

>>> import openturns.coupling_tools as ct
>>> # write a template file
>>> with open('prgm.dat.in', 'w') as f:
...     count = f.write('E=@E_VAR F=-PF-')
>>> # replace tokens from template
>>> ct.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

Examples using the function

Link to a computer code with coupling tools

Link to a computer code with coupling tools