I was curious about it, so make a note.
You can save the data as binary data using the pickle module.
So far good. It's convenient because the usage is explained on various sites.
But there is no information about it at all.
with open("test.pickle","wb") as f:
for i in range(10):
data = {i:"<<<data%d>>>" % i}
pickle.dump(data,f)
The following information was stored in this test.pickle file.
{0: '<<<data0>>>'}
{1: '<<<data1>>>'}
{2: '<<<data2>>>'}
{3: '<<<data3>>>'}
{4: '<<<data4>>>'}
{5: '<<<data5>>>'}
{6: '<<<data6>>>'}
{7: '<<<data7>>>'}
{8: '<<<data8>>>'}
{9: '<<<data9>>>'}
To load () them
with open("test.pickle","wb") as f:
for i in range(10):
yield pickle.load(f)
I wish I could do something like that,
For example, I just want the third one! I wonder if I have to read it from above. It seems that the usage around there is not rolling.
If anyone knows, please teach me.
def get():
with open("test.pickle","rb") as f:
while True:
try:
yield pickle.load(f)
except:
break
data = get()
d = list(data)[3]
print(d)
{3: '<<<data3>>>'}
With this, I was able to get only the 4th data safely, but is it a memory-friendly method?
Recommended Posts