Ping Yeh wrote:
> 2007/9/5, Eric Firing <efiring@...>:
>> Ping Yeh wrote:
>> [snip]
>>> If there is no existing modules for this I'll go ahead write one. :)
>> There is nothing quite like this. As a starting point, though, you
>> should become familiar with the ability of numpy to handle record
>> arrays; your table data type sounds like a numpy record array.
>> Matplotlib is a plotting library built on the numpy Ndimensional array
>> library.
>>
>> Eric
>>
>
> Hi Eric,
>
> Thanks for the info. I searched for the numpy record array and it is
> indeed very close to what I want for the data storage! Thanks!
>
> Just one quick question, though (I admit I don't have much experience
> with numpy...). Can a record array store string data? I'll have some
> of these in the attribute data, not used for plotting but needed for
> other purposes.
Yes. Here is an example:
In [27]:dd1 = numpy.dtype({'names': ['x', 'y', 'i', 'comment'],
'formats': ['<f8', '<f8', '<i4', 'S20']})
In [28]:xx = numpy.array([(1.1, 2.2, 1, 'first'), (1.2, 2.3, 2,
'second')], dtype=dd1)
In [29]:xx
Out[29]:
array([(1.1000000000000001, 2.2000000000000002, 1, 'first'),
(1.2, 2.2999999999999998, 2, 'second')],
dtype=[('x', '<f8'), ('y', '<f8'), ('i', '<i4'), ('comment',
'S20')])
In [30]:xx['x']
Out[30]:array([ 1.1, 1.2])
In [31]:xx['comment']
Out[31]:
array(['first', 'second'],
dtype='S20')
There is one nonobvious thing to note: in line 28, each record must be
a *tuple*, not a *list*.
Note also that you must allow enough space for the maximum length of
string. Alternatively, the strings can be stored as objects:
In [32]:dd2 = numpy.dtype({'names': ['x', 'y', 'i', 'comment'],
'formats': ['<f8', '<f8', '<i4', 'O']})
In [33]:xx = numpy.array([(1.1, 2.2, 1, 'first'), (1.2, 2.3, 2,
'second')], dtype=dd2)
In [34]:xx['comment']
Out[34]:array([first, second], dtype=object)
In [35]:xx['x']
Out[35]:array([ 1.1, 1.2])
Eric
