get_value

get_value(filename, token=None, skip_token=0, skip_line=0, skip_col=0, col_sep=None, encoding='utf-8')

Get a value from a file using a delimiter and/or offsets.

This function is optimized to be rather fast and takes low memory on human readable file.

Parameters:
filenamestr

The name of the file that will be parsed

tokenstr

A regex that will be searched. The value right after the token is returned. Default: None (no token searched)

skip_tokenint

The number of tokens that will be skipped before getting the value. If set to != 0, the corresponding token parameter must not be equal to None. If skip_tokens < 0: count tokens backward from the end of the file. Default: 0: no token skipped

skip_lineint

Number of lines skipped from the token found. If corresponding token equal None, skip from the beginning of the file. If corresponding token != None, skip from the token. If skip_line < 0: count lines backward from the token or from the end of the file. Be careful: a last empty line is taken into account too. Default: 0: no line skipped

skip_colint

Number of columns skipped from the token found. If corresponding token = None, skip words from the beginning of the line. If corresponding token != None, skip words from the token. If skip_col < 0: count col backward from the end of the line or from the token. Default: 0: no column skipped

col_sepstr

Column separator Default: None: whitespace separator, see str.split

encodingstr

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

Returns:
valuefloat

Value found

Raises:
AssertionError

parameters badly set

EOFError

no value found

Examples

using a single token

>>> import openturns.coupling_tools as ct
>>> with open('results.out', 'w') as f:
...     count = f.write('Y1=2.0, Y2=-6.6E56')
>>> ct.get_value('results.out', token='Y1=')
2.0

using token and skip_tokens

>>> with open('results.out', 'w') as f:
...     count = f.write('Y1=2.6 Y1=6.0 # temperature 2')
>>> ct.get_value('results.out', token='Y1=', skip_token=1)
6.0

using column & line

>>> with open('results.out', 'w') as f:
...     count = f.write('1.1 1.2 1.3 1.4')
>>> ct.get_value(filename='results.out', skip_col=2)
1.3

Examples using the function

Link to a computer code with coupling tools

Link to a computer code with coupling tools