Python:将应为数组的字符串转换回数组

时间:2018-08-22 00:46:36

标签: python arrays pandas nan

我有一个字符串,应该是3x3x3数组。只是寻找一种简单的方法来将此转换回其“真相”,以便我可以访问这些值。它是换行符,[]和我认为的制表符,但有7或6个空格。

我以为可以将数组保存到pandas DataFrame中,但是给了我这个:

'[[[      nan       nan       nan]\n  [      nan       nan      
nan]\n  [      nan       nan       nan]]\n\n [[ 0.005506  0.005506    
nan]\n  [ 0.006684       nan       nan]\n  [ 0.006684       nan      
nan]]\n\n [[      nan       nan       nan]\n  [      nan       nan    
nan]\n  [      nan       nan       nan]]]'

我尝试了.split('\n')和其他各种分隔符和组合,但收效甚微。

寻找数组(仅是示例):

x = [[[0,0,0],[0,0,0],[0,0,0]],[[1,1,1],[1,1,1],[1,1,1]],[[2,2,2],[2,2,2],[2,2,2]]]

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以尝试使用Python附带的json library
具体来说,您将需要使用json.loads()函数。请记住,仍使用library(stringr) str_split(strings, "\\B(?=[a-z|0-9][A-Z])")

[[1]] [1] "This is a phrase with a Nam" "eThis is another phrase" [[2]] [1] "This is a phrase with the number 201" "9This is another phrase" 分割字符串

以下是有关如何使用它的示例:

$abc = ['a', 'b'];

array_push($abc, 'c', 'd');

print_r($abc);

答案 1 :(得分:0)

eval()的一种罕见用法:

s = '[[[0,0,0],[0,0,0],[0,0,0]],[[1,1,1],[1,1,1],[1,1,1]],[[2,2,2],[2,2,2],[2,2,2]]]'
x = eval(s)
print(x) #[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[2, 2, 2], [2, 2, 2], [2, 2, 2]]]

编辑:如前所述,评估不足以满足您的要求。我最终得到的工作是基于json和numpy构建的

s = '''[[[ nan nan nan]\n [ nan nan\nnan]\n [ nan nan nan]]\n\n [[ 0.005506 0.005506\nnan]\n [ 0.006684 nan nan]\n [ 0.006684 nan\nnan]]\n\n [[ nan nan nan]\n [ nan nan\nnan]\n [ nan nan nan]]]'''

import numpy, json
x = numpy.array(json.loads(','.join(s.split()).replace('[,','[').replace('nan','NaN')))

print(x)

#array([[[      nan,       nan,       nan],
#        [      nan,       nan,       nan],
#        [      nan,       nan,       nan]],
#       [[ 0.005506,  0.005506,       nan],
#        [ 0.006684,       nan,       nan],
#        [ 0.006684,       nan,       nan]],
#       [[      nan,       nan,       nan],
#        [      nan,       nan,       nan],
#        [      nan,       nan,       nan]]])

您可以轻松地将numpy.array()替换为pandas.DataFrame()