68. plugins.datareader — Numerical data reader

68.1. Functions defined in module plugins.datareader

plugins.datareader.splitFloat(s)[source]

Match a floating point number at the beginning of a string.

Parameters:

s (str) – A string that starts with a numerical part (float).

Returns:

tuple (float, str) | None – If the beginning of the string matches a floating point number, returns a tuple with the float and the remainder of the string. Else, return None

Examples

>>> splitFloat('123e4rt345e6')
[1230000.0, 'rt345e6']
plugins.datareader.readData(s, to, strict=False)[source]

Read values from a string matching the units/type specifications.

This is a powerful function for reading, interpreting and converting numerical data from a string.

Parameters:
  • s (str) – A string containing comma-separated fields. Each field is either a numerical value, or a numerical value followed by a units string. The value and the units can optionally be separated by whitespace.

  • to (list of str) – A list of units to which the fields in the string s should be converted. The length of the list is usually equal to the number of fields in the string s. If a field has units, the corresponding to should be a compatible unit (i.e. for the same physical quantity). If a field has no units, the target to value can also be ‘int’ or ‘float’.

  • strict (bool) – If True, the length of the to list should exactly match the number of fields in s. The default (False) allows unequal lengths and will only proceed until the shortest is exhausted.

Returns:

list – A list of int/float values read from the string s. With strict=True the list will have the same length as the number of fields in s and the number of items in to. Else, it will have the shortest length of the two. Where the field contains a units designator, the value is converted to the requested to units. designator.Fields in the string s are separated by

Examples

>>> readData('1 inch', ['mm'])
[25.4]
>>> readData('12, 13s, 14.5e3, 12 inch, 1hr, 31kg, 5MPa',
...     ['int','float','kg','cm','s','g','kN/m**2'])
[12, 13.0, 14500.0, 30.48, 3600.0, 31000.0, 5000.0]

Warning

You need to have the GNU units command installed for the unit conversion to work.

plugins.datareader.convertData(s, to)[source]

Convert numerical data to a requested type or units.

Converts a string including a numerical value and optional units to the requested type or units.

Parameters:
  • s (str) – A string containing a numerical value and an optional units string, optionally separated by whitespace. E.g. ‘10 kg’.

  • to (str) – The units or type to which the value from the string s is to be converted. If s contains units, the specified to units should be compatible (i.e. for the same physical quantity). If a field has no units, the target to value can also be ‘int’ or ‘float’.

Returns:

int | float – The numerical value read from the string s, converted to the specified units or type to.

Examples

>>> convertData('1 inch', 'mm')
25.4
>>> convertData('1', 'mm')
1.0
>>> convertData('14.5e3', 'kg')
14500.0
>>> convertData('12 inch', 'float')
12.0
>>> convertData('17.5', 'int')
17

Warning

You need to have the GNU units command installed for the unit conversion to work.