0

I need to save some data from Python and read it later with both Python and Matlab. The obvious choice seems to be scipy.io.savemat. But look what happens:

>>> a = {'foo': 0, 'bar': [1,2,3,4,5], 'baz': {'key1': range(10), 'key2': 'Oh No
 Mr Bill!'}}
>>> scipy.io.savemat('a.mat',a)
>>> b = scipy.io.loadmat('a.mat')
>>> b['foo']
array([[0]])
>>> b['bar']
array([[1],
       [2],
       [3],
       [4],
       [5]])
>>> b['baz']
array([[([u'Oh No Mr Bill!'], [[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
)]],
      dtype=[('key2', '|O4'), ('key1', '|O4')])
>>> b['baz']['key1']
array([[[[0]
 [1]
 [2]
 [3]
 [4]
 [5]
 [6]
 [7]
 [8]
 [9]]]], dtype=object)
>>> b['baz']['key2']
array([[[u'Oh No Mr Bill!']]], dtype=object)
>>> b['baz']['key2'][0,0]
array([u'Oh No Mr Bill!'],
      dtype='<U14')

Everything seems to be stored as 2D arrays for each dictionary... (e.g. foo['baz']['key1'] shows up as a 4D array) Is this a bug or is there a reason for this behavior?

4

1 回答 1

0

有没有理由不能将pickleormarshall模块与 matlab 格式结合使用?

例如,以 matlab 格式保存数据(丢失结构信息(例如 dicts))并同时腌制它以无损重新导入 python

于 2013-05-31T15:59:55.040 回答