私はHDF5を使用することをお勧めします。彼らはIOのために非常に高速です。ここ はあなたのデータを書き込む方法です:
import numpy as np
import tables
fname = 'myOutput.h5'
length = 100 # your data length
my_data_generator = xrange(length) # Your data comes here instead of the xrange
filters = tables.Filters(complib='blosc', complevel=5) # you could change these
h5file = tables.open_file(fname, mode='w', title='yourTitle', filters=filters)
group = h5file.create_group(h5file.root, 'MyData', 'MyData')
x_atom = tables.Float32Atom()
x = h5file.create_carray(group, 'X', atom=x_atom, title='myTitle',
shape=(length,), filters=filters)
# this is a basic example. It will be faster if you write it in larger chunks in your real code
# like x[start1:end1] = elements[start2:end2]
for element_i, element in enumerate(my_data_generator):
x[element_i] = element
h5file.flush()
h5file.close()
それが使用読みについて:
h5file = tables.open_file(fname, mode='r')
x = h5file.get_node('/MyData/X')
print x[:10]
結果:
marray([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)
Pythonの組み込みファイルの読み書きはこれを達成しませんでしたか? – lucasnadalutti
もちろん、私はそれをやるより効率的な方法があるのだろうかと思います。私の投稿を編集しています。 –
@ P-M同じファイルに保存するのはなぜ重要なのですか? – MZHm