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. When dealing with overlapping tokens, longest tokens should go 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