replace

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

Replace tokens in a file.

Parameters:

infile : str, template file that will be parsed

outfile : str, file that will received the template parsed.

If equal to “None” or to “infile”, the result file will be moved to infile.

tokens : list, regexes that will be replaced

values : list, values (can be string, float, …) that will replace

the tokens. The list must have the same size as tokens.

formats : list, 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, file encoding

Raises:

AssertionError

parameters badly set

EOFError

a token has not been found

Examples

template.in = ‘E=@E_VAR F=-PF-‘)

>>> # replace(infile="template.in",
>>> #    outfile="prgm_data.in",
>>> #    tokens=["@E_VAR", ".PF."],
>>> #    values=[1.4, "5"])
>>> # prgm_data.in = 'E=1.4 F=5'

be careful with overlapping tokens !! template.in = ‘E=@E EE=@EE’)

>>> # replace(infile="template.in",
>>> #    outfile="prgm_data.in",
>>> #    tokens=["@E", "@EE"],
>>> #    values=[1, 2])

It raises exception!! -> @EE token not found! (this is due to the first pass with token “@E” that modify prgm_data.in like this : ‘E=1 EE=1E’)

deals with overlapping tokens : put longest tokens first template.in = ‘E=@E EE=@EE’)

>>> # replace(infile="template.in",
>>> #    outfile="prgm_data.in",
>>> #    tokens=["@EE", "@E"],
>>> #    values=[2, 1])
>>> # prgm_data.in = 'E=1 EE=2')