Skip to content

Commit ea081e7

Browse files
committed
added object serialization tutorial
1 parent c4b7755 commit ea081e7

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
5050
- [How to Download Files in Python](https://www.thepythoncode.com/article/download-files-python). ([code](general/file-downloader))
5151
- [How to Compress and Decompress Files in Python](https://www.thepythoncode.com/article/compress-decompress-files-tarfile-python). ([code](general/compressing-files))
5252
- [How to Extract PDF Tables in Python](https://www.thepythoncode.com/article/extract-pdf-tables-in-python-camelot). ([code](general/pdf-table-extractor))
53+
- [How to Use Pickle for Object Serialization in Python](https://www.thepythoncode.com/article/object-serialization-saving-and-loading-objects-using-pickle-python). ([code](general/object-serialization))
5354

5455
- ### [Web Scraping](https://www.thepythoncode.com/topic/web-scraping)
5556
- [How to Access Wikipedia in Python](https://www.thepythoncode.com/article/access-wikipedia-python). ([code](web-scraping/wikipedia-extractor))
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Use Pickle for Object Serialization in Python](https://www.thepythoncode.com/article/object-serialization-saving-and-loading-objects-using-pickle-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pickle
2+
3+
# define any Python data structure including lists, sets, tuples, dicts, etc.
4+
l = list(range(10000))
5+
6+
# save it to a file
7+
with open("list.pickle", "wb") as file:
8+
pickle.dump(l, file)
9+
10+
# load it again
11+
with open("list.pickle", "rb") as file:
12+
unpickled_l = pickle.load(file)
13+
14+
15+
print("unpickled_l == l: ", unpickled_l == l)
16+
print("unpickled l is l: ", unpickled_l is l)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import pickle
2+
3+
class Person:
4+
def __init__(self, first_name, last_name, age, gender):
5+
self.first_name = first_name
6+
self.last_name = last_name
7+
self.age = age
8+
self.gender = gender
9+
10+
def __str__(self):
11+
return f"<Person name={self.first_name} {self.last_name}, age={self.age}, gender={self.gender}>"
12+
13+
14+
p = Person("John", "Doe", 99, "Male")
15+
16+
# save the object
17+
with open("person.pickle", "wb") as file:
18+
pickle.dump(p, file)
19+
20+
# load the object
21+
with open("person.pickle", "rb") as file:
22+
p2 = pickle.load(file)
23+
24+
print(p)
25+
print(p2)
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pickle
2+
3+
4+
class Person:
5+
def __init__(self, first_name, last_name, age, gender):
6+
self.first_name = first_name
7+
self.last_name = last_name
8+
self.age = age
9+
self.gender = gender
10+
11+
def __str__(self):
12+
return f"<Person name={self.first_name} {self.last_name}, age={self.age}, gender={self.gender}>"
13+
14+
p = Person("John", "Doe", 99, "Male")
15+
16+
# get the dumped bytes
17+
dumped_p = pickle.dumps(p)
18+
print(dumped_p)
19+
20+
# write them to a file
21+
with open("person.pickle", "wb") as file:
22+
file.write(dumped_p)
23+
24+
# load it
25+
with open("person.pickle", "rb") as file:
26+
p2 = pickle.loads(file.read())
27+
28+
print(p)
29+
print(p2)
30+

0 commit comments

Comments
 (0)