win7, x64, Python 2.7.12
数据格式为
myData = [[a1, b1, c1, d1, e1, f1, g1, h1], [a2, b2, c2, .... ], ..... ]
,其中myData
是浮点数的np.ndarray
。我使用下面的命令保存了它…
with open('myData.txt', 'w') as f:
for s in myData:
f.write(str(s) + 'n')
经检查,它实际上被保存为…
[a1 b1 c1 d1 e1 f1 g1 h1]
[a2 b2 c2 d2 e2 f2 g2 h1]
.....
。标签分隔。
所以我试着用…
import numpy as np
from ast import literal_eval
with open('myData.txt', 'r') as f:
fromFile = [np.ndarray(literal_eval(line)) for line in f]
f.close()
但是这会抛出一个错误…
File "<unknown>", line 1
[ 1. 1.198 2.063 1.833 1.458 1.885 1.969 0.343]
^
SyntaxError: invalid syntax
所以,我不能再生文件myData.txt
如何恢复到其初始数据类型?
是否有一种方法可以阻止数据在第一时间被写出来?
编辑:上面的一个解决方案…
import numpy as np
from ast import literal_eval
branches = ['[ 1. 1.198 2.063 1.833 1.458 1.885 1.969 0.343]n',
'[ 2. 1.26 2. 1.26 1.26 2. 1.26 0. ]n',
'[ 3. 1.688 2. 1.781 1.573 2.021 1.979 0.23 ]n',
'[ 4. 1.604 2.729 1.792 1.667 2.49 1.948 0.293]n']
branches = [line.rstrip(']n') for line in branches]
branches = [line.lstrip('[ ') for line in branches]
print branches[0]
branches = [line.split(' ') for line in branches]
newBranches = []
for branch in branches:
branch = filter(None, branch)
branch = [float(item) for item in branch]
newBranches.append(branch)
print newBranches[0]
branches = np.array(newBranches)
除非有更快的方法,否则这就是我要做的。我也会采纳Nils Werner在下面的回答中的建议。
你应该使用
numpy.save('myData.npy', myData)
你可以读成
myData = numpy.load('myData.npy')