我的字符串final_line
为
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0130631958731,0.0,0.0,0.0,0.0,0.0,0.0,0.0130631958731,0.0,0.0,0.0,0.0,0.0,0.0,,
0.00507937313707,0.0,0.0,0.0201058520009,0.0,0.0,0.0459562331449,0.0268078026679,0.0,0.0103772139359,0.0,0.0,0.0438673134565,0.0,0.0268078026679,0.0,0.0,0.0,0.0,0.0,0.0224437417685,,
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.070802847389,0.0,0.0,,
0.0,0.0,0.169140135429,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,,
0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0261263917462,0.0,0.0,0.0,0.0,0.0,0.0,0.0261263917462,0.0,0.0,0.0,0.0,0.0,0.0,,
0.0961428138228,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,,
我想将其转换为numpy数组,而不转移到CSV
文件。这里字符串中的每一行都应该转换为不同的行
[['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0.08364751'], ['0.017944717', '0', '0', '0', '0.009470823', '0', '0', '0', '0', '0', '0'], ['0.012620501', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0.012620501', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0.01332164', '0', '0', '0', '0', '0', '0'], ['0.012620501', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '0', '0', '0', '0', '0.097414177', '0.042092545', '0', '0', '0', '0'], ['0', '0', '0', '0', '0.01332164', '0', '0', '0', '0', '0', '0'], ['0.005324215', '0', '0', '0.04598186', '0.028100025', '0', '0', '0', '0', '0', '0.023525603'], ['0', '0', '0', '0', '0', '0', '0', '0', '0.055765006', '0', '0'], ['0', '0', '0.133216404', '0', '0', '0', '0', '0', '0', '0', '0']]
我知道这里的输出有不同的值,但我刚刚显示得到格式
实施例
我有字符串
a1,b1,c1,d1,e1,,
a2,b2,c2,d2,e2,,
a3,b3,c3,d3,e3,,
然后输出
[['a1','b1','c1','d1','e1'],
['a2','b2','c2','d2','e2'],
['a3','b3','c3','d3','e3']]
我已经看到了其他问题,但它只转换为单行数组。在这里,我想要新行中的每一行,如示例所示。
答案 0 :(得分:1)
map
会很有用。另外,,,
中的最后一个final list
不是必需的,所以我忽略了它(在:-2
操作之前查看split
。
import numpy as np
A = final_line[:-2].split(',,')
B = np.array([map(float,a.split(',')) for a in A])
答案 1 :(得分:0)
您的数据采用列表形式,每行以另外一个逗号分隔。
d_in = "1,2,3,,4,5,6,,7,8,9"
first = d_in.split(",,")
final = [line.split(",") for line in first]
first
存储字符串列表(每个列)。 final
以适当的格式存储您的数据(在我的示例中为[[1,2,3],[4,5,6],[7,8,9]]
)