0

从该getValues()方法中,AFXComTableKeyword该类返回一个字符串,如:

test = "('mat_huehne_2008', '0.125', '24.0'),('', '', '-24.0'),('', '', '41.0')"+\
       ",('', '', '-41.0'),('', '', ''),('', '', ''),('', '', ''),('','', ''),"+\
       "('', '', ''),('', '', ''),('', '', ''),('', '', ''),('', '', ''),"+\
       "('', '', ''),('', '', ''),('', '', ''),('', '', ''),('', '', ''),"+\
       "('', '', ''),('', '', ''),('', '', ''),('', '', ''),('', '', ''),"+\
       "('', '', ''),('', '', ''),('', '', ''),('', '', ''),('', '', ''),"+\
       "('', '', ''),('', '', ''),('', '', ''),('', '', ''),('', '', ''),"+\
       "('', '', ''),('', '', ''),('', '', ''),('', '', ''),('', '', ''),"+\
       "('', '', ''),('', '', '')"

最后,我们需要一个numpy.arrayof shape=(40,3),对于这种情况,它看起来像:

 test = [['mat_huehne_2008', '0.125',  '24.0'],
         [               '',      '', '-24.0'],
         ... and so on ... ]

有谁知道在 Python / Numpy 中执行此操作的快捷方式?

4

1 回答 1

3

我会用来ast将字符串评估为元组。那么这很容易:

>>> import ast
>>> import numpy as np
>>> np.array(ast.literal_eval(test))
array([['mat_huehne_2008', '0.125', '24.0'],
       ['', '', '-24.0'],
       ['', '', '41.0'],
       ['', '', '-41.0'],
       ...
于 2013-05-06T13:08:21.253 回答