Not much mention of how to use Pickle

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.

Postscript

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

Not much mention of how to use Pickle
Summary of how to use pandas.DataFrame.loc
Summary of how to use csvkit
[Python] Summary of how to use pandas
How to calculate Use% of df command
[Python2.7] Summary of how to use unittest
Jupyter Notebook Basics of how to use
Basics of PyTorch (1) -How to use Tensor-
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
[Question] How to use plot_surface of python
How to use Python-shell
How to use folium (visualization of location information)
How to use tf.data
A simple example of how to use ArgumentParser
How to use virtualenv
How to use Seaboan
[Python] How to use two types of type ()
How to use shogun
How to use Pandas 2
How to use Virtualenv
How to use numpy.vectorize
How to use pytest_report_header
How to use partial
How to use Bio.Phylo
Summary of how to use MNIST in Python
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to use IPython
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to use list []
How to use python-kabusapi
How to use OptParse
How to use return
How to use dotenv
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
A memo of how to use AIST supercomputer ABCI
I tried to summarize how to use matplotlib of python
Memo of how to use properly when combining pandas.DataFrame
How to use Python Kivy ① ~ Basics of Kv Language ~
How to use Qt Designer
How to use search sorted
[gensim] How to use Doc2Vec
python3: How to use bottle (2)
Understand how to use django-filter
How to use the generator
[Python] How to use list 1
How to use FastAPI ③ OpenAPI
How to use Python argparse
How to use IPython Notebook