Win 7,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的建议。
答案 0 :(得分:1)
你应该使用
numpy.save('myData.npy', myData)
然后您可以像
那样阅读myData = numpy.load('myData.npy')