Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
bd468103
Kaydet (Commit)
bd468103
authored
Tem 28, 2006
tarafından
Andrew M. Kuchling
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add example
üst
4036f43c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
44 additions
and
1 deletion
+44
-1
libpickle.tex
Doc/lib/libpickle.tex
+44
-1
No files found.
Doc/lib/libpickle.tex
Dosyayı görüntüle @
bd468103
...
@@ -725,7 +725,50 @@ source of the strings your application unpickles.
...
@@ -725,7 +725,50 @@ source of the strings your application unpickles.
\subsection
{
Example
\label
{
pickle-example
}}
\subsection
{
Example
\label
{
pickle-example
}}
Here's a simple example of how to modify pickling behavior for a
For the simplest code, use the
\function
{
dump()
}
and
\function
{
load()
}
functions. Note that a self-referencing list is pickled and restored
correctly.
\begin{verbatim}
import pickle
data1 =
{
'a': [1, 2.0, 3, 4+6j],
'b': ('string', u'Unicode string'),
'c': None
}
selfref
_
list = [1, 2, 3]
selfref
_
list.append(selfref
_
list)
output = open('data.pkl', 'wb')
# Pickle dictionary using protocol 0.
pickle.dump(data1, output)
# Pickle the list using the highest protocol available.
pickle.dump(selfref
_
list, output, -1)
output.close()
\end{verbatim}
The following example reads the resulting pickled data. When reading
a pickle-containing file, you should open the file in binary mode
because you can't be sure if the ASCII or binary format was used.
\begin{verbatim}
import pprint, pickle
pkl
_
file = open('data.pkl', 'rb')
data1 = pickle.load(pkl
_
file)
pprint.pprint(data1)
data2 = pickle.load(pkl
_
file)
pprint.pprint(data2)
pkl
_
file.close()
\end{verbatim}
Here's a larger example that shows how to modify pickling behavior for a
class. The
\class
{
TextReader
}
class opens a text file, and returns
class. The
\class
{
TextReader
}
class opens a text file, and returns
the line number and line contents each time its
\method
{
readline()
}
the line number and line contents each time its
\method
{
readline()
}
method is called. If a
\class
{
TextReader
}
instance is pickled, all
method is called. If a
\class
{
TextReader
}
instance is pickled, all
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment