Pickling
Text files are convenient to use because you can read, write, and append them with any text editor, but they are limited to storing a series of characters. Sometimes you may want to store complex information such as list and dictionary. Here we would use Python's pickle
module. The pickle
module is used to store complex data such as list and dictionary. Let's discuss with the help of an example:
import pickle name = ["mohit","bhaskar", "manish"] skill = ["Python", "Python", "Java"] pickle_file = open("emp1.dat","w") pickle.dump(name,pickle_file) pickle.dump(skill,pickle_file) pickle_file.close()
The program seems complex to understand. Let's understand the code line by line.
The name
and skill
are two lists which we have to store.
The pickle_file = open("emp1.dat","w")
syntax creates a pickle_file
object in write mode as we have done earlier.
The pickle.dump()
is used to dump and store the lists name
and skill
in the pick.dat
file. The pickle.dump()
requires two arguments, first the...