Serialization¶
What is serialization?¶
Objects, variables, data in the program are volatile, stored in RAM. After switching off the program - they disappear. Serialization is about saving an existing object in permanent memory - to a file, database, etc.
Pickle module¶
The easiest way to serialize is to use the pickle module, which transforms any object into a series of bytes. Then this byte stream can be transferred or saved and later reconstructed to create a new object with the same properties.
Serialization example¶
import pickle
data = {
'a': [1, 2.0, 3, 4+6j],
'b': ("Alice has a cat", "Python programming is great"),
'c': [False, True, False]
}
with open('data.pickle', 'wb') as f:
pickle.dump(data, f)
As we can see, we can serialize data of various types: strings, logical values, numeric values, and even objects such as lists and dictionaries. We need to remember to pass the mode to 'wb' in the open function, because pickle converts objects into a series of bytes.
Deserialization example¶
import pickle
with open('data.pickle', 'rb') as f:
data = pickle.load(f)
print(data)
The result of executing the above code will be:
{'a': [1, 2.0, 3, (4+6j)], 'b': ('Alice has a cat', 'Python programming is great'), 'c': [False, True, False]}
which is exactly what we wrote above to this file. As with saving, here we must remember to add 'b' to the file open mode in the open function.
CSV¶
Another file format to which we can save data in Python is CSV (comma-separated values). This time, however, it is a format for storing data in text files.
Here we see an example of data in this format, opened in a text editor. As the name says, these are values separated by a comma.
And here we see the same CSV file, but opened in a spreadsheet (here in LibreOffice) - this software treats a comma as a separator and places the data separated by it in separate columns.
Example of reading from a CSV file¶
import csv
with open("employees.csv") as in_file:
reader = csv.reader(in_file)
for row in reader:
print(row)
WThe result of the above code will appear in the console:
['employee', 'salary']
['John Smith', '2500']
['Jenny Scoot', '7500']
['Kate Noris', '10000']
Example of saving to a CSV file¶
import csv
with open("employees.csv", 'a') as out_file:
writer = csv.writer(out_file)
writer.writerow(["Jenny Scoot", 2500])
As before, we use the open function to open and access the employees.csv file - this time with the 'a' flag so we can write to it. Next, we create the csv.writer object that will allow us to write another line to this file.
Execution of the above code will add the next line in the "employees.csv" file, where the text "Jenny Scoot" will appear in the first column, and the value 2500 in the second column.
JSON¶
JSON (ang. JavaScript Object Notation) is another way to store and transmit text data. Often used for communication and information transfer when creating and using web APIs (e.g. in the REST standard).
An example of a JSON file is shown above. As we can see, it has a certain key-value structure. A single value or collection can be written under a given key.
An example of reading from a JSON file¶
import json
with open("example.json") as in_file:
data = json.load(in_file)
print(data['dogs'])
The above code will print the value under the key 'dogs' from the dictionary in the picture.
[{'id': 0, 'name': 'maja', 'race': 'pies'}, {'id': 1, 'name': 'milus', 'race': 'pies'}]
An example of writing to a JSON file¶
import json
students = [
{
'name': "John",
'surname': "Smith",
'score': 20
},
{
'name': "Kevin",
'surname': "Scoot",
'score': 17
}
]
with open("students.json", 'w') as out_file:
json.dump(students, out_file, indent=2)
Calling the above code will create the file "students.json" with the written information from the list created directly in the code.
[
{
"name": "John",
"surname": "Smith",
"score": 20
},
{
"name": "Kevin",
"surname": "Scoot",
"score": 17
}
]