Saving Numeric arrays to file?

Duncan Smith buzzard at urubu.freeserve.co.uk
Fri Sep 26 20:21:03 EDT 2003


I am having problems saving arrays to file.  e.g.

>>> t1
array([30])
>>> t2
array([ 0,  0,  5,  5,  5, 10])
>>> t3
array([20,  5])
>>> t4
array([[ 0,  5, 10,  5,  0, 10],
       [ 0,  0,  0,  5,  0,  0]])
>>> f = file('c:\Data\IS_rounded.txt', 'w')
>>> f.write(t1.tostring() + '\n')
>>> f.write(t2.tostring() + '\n')
>>> f.write(t3.tostring() + '\n')
>>> f.write(t4.tostring() + '\n')
>>> f.close()
>>>
>>> f = file('c:\Data\IS_rounded.txt', 'r')
>>> Numeric.fromstring(f.readline().strip())
array([30])
>>> Numeric.fromstring(f.readline().strip())
array([0, 0, 5, 5, 5])                                        # missing
final element
>>> Numeric.fromstring(f.readline().strip())

Traceback (most recent call last):
  File "<pyshell#181>", line 1, in -toplevel-
    Numeric.fromstring(f.readline().strip())
ValueError: string size must be a multiple of element size
>>>

Anyone any idea where I'm going wrong?  2.3 on Win2k.  Cheers.

Duncan






More information about the Python-list mailing list