Conversion between numbers and strings

The prefix number_strings (or the alias ns) is needed as to resolve the names of objects defined in this module.

Loss of precision

Problems can arise when converting between numbers and strings. Python conversions can truncate the number of floating-point digits, which introduces errors.

The functions in this module can be used to avoid unintended numerical errors.

Functions

  • to_string(x) : converts x to a string
  • sequence_printer(seq) : uses to_string(x) to convert the elements of a sequence
  • to_numeric(x) : converts x to a number
  • sequence_parser(seq) : uses to_numeric(x) to convert the elements of a sequence

Module contents

class File(filename, mode, delim=None)

Provides methods for reading and writing numeric data

Numbers are converted to, or from, text without loss of precision.

Other functionality defaults to standard Python file behaviour.

Examples:

# read a file
with ns.File('my_file','r',delim=',') as file:  
    for line in file: print line

# or ...
file = ns.File('my_file','r',delim=',')   
for line in file: print line
newline()

Put a newline character in the file

read(delim=None)

Return file contents as a sequence

All strings are converted to numbers, if possible

delim separates elements in the line (delim is set when opening the file [default: whitespace])

readline(delim=None)

Return one line as a sequence

All strings are converted to numbers, if possible

delim separates elements in the line [default: whitespace] (delim can also be set when opening the file )

readlines(delim=None)

Return file as a sequence of line sequences

All strings are converted to numbers, if possible

delim separates elements in the line [default: whitespace] (delim can also be set when opening the file )

write(seq, delim=None, imag_unit='j')

Write seq to the file

Numbers in seq are converted to strings without loss of precision.

write does not add a line separator at the end of the write sequence (use newline).

to_string(x, imag_unit='j')

Return a string representing x

Parameters:
  • x – anything
  • imag_uniti or j
Returns:

a string representation of x

The string format generated for complex numbers is recognised by spreadsheet applications.

For non-numeric types repr is used to generate a string.

Examples:

>>> z = 1.0/3.0 - 0.3134j
>>> ns.to_string(z)
'0.3333333333333333-0.3134j'

>>> x = float(3.141414E-21)
>>> ns.to_string(x)
'3.141414e-21'
sequence_printer(seq, imag_unit='j')

Return a list of strings

Parameters:seq – a sequence, or nested sequences
Returns:a list of strings corresponding to of the elements in seq

Numeric elements in seq retain the internal floating point precision (see to_string).

If seq contains nested sequences, the elements will be converted recursively and the lists returned will also be nested.

Example:

>>> x = la.array( [ 0.123 * i for i in range(12) ])
>>> x.shape = 2,2,3
>>> print x                 # Python string conversion limits precision
[[[0.0 0.123 0.246]
[0.369 0.492 0.615]]
[[0.738 0.861 0.984]
[1.107 1.23 1.353]]]
>>> ns.sequence_printer(x)     # precision conserved
[[['0.0', '0.123', '0.246'], 
['0.369', '0.492', '0.615']], 
[['0.738', '0.861', '0.984'], 
['1.107', '1.23', '1.353']]
]
to_numeric(x)

Return x as a number, if possible

Parameters:x – string
Returns:a number, if possible, otherwise x

Examples:

>>> x = '3.141413999999999e-21'
>>> ns.to_numeric(x)
3.141413999999999e-21

>>> z = '0.3333333333333333-0.3134j'
>>> ns.to_numeric(z)
(0.3333333333333333-0.3134j)
sequence_parser(seq)

Return a list of numbers

Parameters:seq – a sequence of strings
Returns:a list of strings and numbers

Elements in seq are converted to numbers where possible, otherwise the original is returned (see to_numeric).

If seq contains nested sequences, elements will be converted recursively and nested sequences are returned.

Example:

>>> x = [['0.0', '0.123', '0.246'],
... ['0.3689999999999999', '0.4919999999999999', '0.6149999999999999']]
>>> ns.sequence_parser(x)
[[0.0, 0.123, 0.246], [0.3689999999999999, 0.4919999999999999, 0.6149999999999999]]
open(filename, mode=None, delim=None)

Return a File object for handling numeric data

Parameters:
  • filename – file name
  • mode – opening mode
  • delim – file delimiting character

Note

This function is deprecated. Use File instead, or use use File in a with ... as statement.

Example:

# open CVS file and print contents
>>> file = ns.open('my_file','r',delim=',')   
>>> for line in file:
...     print line
...