When we work on Binary files in Python conversion of data at the time of reading and writing is required. Pickling modules allow us to store any kind of object in a file and it allows us to store python objects with their structures. So for storing the data in Binary format we use Pickle Module.
In this tutorial we will cover the following topics:
Pickling means converting of python objects structure into byte stream which is also known as marshaling or flattening.
For reading a file a reverse process unpickling is used to convert byte stream to python object structure.
To use the Pickle module first we import the pickle module which will provide two methods dump and load.
For the creation of the binary files, we use pickle.dump() to write the objects in a file.
Syntax to import Pickle Module:
import pickle
In this tutorial we will cover the following topics:
- What is Pickle Module?
- How to use Pickle Module?
- How to write data in binary file?
- Program to write List in the binary file.
- How to read a binary file?
What is Pickle Module?
Pickling means converting of python objects structure into byte stream which is also known as marshaling or flattening.
For reading a file a reverse process unpickling is used to convert byte stream to python object structure.
How to use Pickle Module?
To use the Pickle module first we import the pickle module which will provide two methods dump and load.
For the creation of the binary files, we use pickle.dump() to write the objects in a file.
Syntax to import Pickle Module:
import pickle
How to write in Pickle Module?
To write in binary file we use dump method which will take object and file-object as parameter.
Syntax to use dump() in Python:
dump(object,fileobject)
Here object is the variable in which data is stored and file object is the variable in which file is opened.
For example:
If we want to write a string in the binary file we use:
import pickle
object = "Hello World"
fileobject = open("test121.txt",'wb')
pickle.dump(object,fileobject)
print("String added to the Binary File")
fileobject.close()
Syntax to use dump() in Python:
dump(object,fileobject)
Here object is the variable in which data is stored and file object is the variable in which file is opened.
For example:
If we want to write a string in the binary file we use:
import pickle
object = "Hello World"
fileobject = open("test121.txt",'wb')
pickle.dump(object,fileobject)
print("String added to the Binary File")
fileobject.close()
Program to write List in the binary file
To write List in a binary file:
- First, we have to import pickle module.
- Then, we have to open a file.
- Then, we store a list in a variable.
- Then, we use pickle dump method - dump(variable name , fileobject).
- Then, we close the file.
import pickle
list = [10,20,30,40,50]
fileobject = open("test122.txt",'wb')
pickle.dump(list,fileobject)
print("List added to the Binary File")
fileobject.close()
How to read a binary file?
We have stored data in a binary file using dump() method and it can be used for reading using load() method. It will take fileobject as a parameter.
import pickle
fileobject = open("test121.txt",'rb')
data = pickle.load(fileobject)
fileobject.close()
print(data)
-Thankyou-
For any Query Comment below or Contact Us
0 Comments