Data serialization is the process of converting structured data to a format that allows us to share or store the data in a form that allows recovery of its original structure. In some cases, the secondary intention of data serialization is to minimize the data’s size which then reduces disk space or bandwidth requirements.
This post will demonstrate how to run another Python feature in Python4Delphi with RAD Studio: Serialization in Python GUI apps and gets the output.
Prerequisites: Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in this video Getting started with Python4Delphi.
First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.
Table of Contents
1. Flat vs Nested Data
Before beginning to serialize data, it is important to identify or decide how the data should be structured during data serialization – flat or nested. The differences in the two styles are shown in the below examples.
Flat style:
1 |
{ "Type" : "A", "field1": "value1", "field2": "value2", "field3": "value3" } |
Nested style:
1 2 |
{"A" {"field1": "value1", "field2": "value2", "field3": "value3" }} |
2. Serializing Text
2.1. Simple File (Flat Data)
If the data that we want to be serialized is located in a file and contains flat data, Python offers repr to serialize data:
repr
The repr method in Python takes a single object parameter and returns a printable representation of the input:
1 2 3 4 5 |
# Input as flat text a = { "Name" : "Abraham", "Age": "23", "Country": "Albania", "Hobby": "Python Programming" } # Returns a printable representation of the input; print(repr(a)) |
Output in Python GUI:
2.2. CSV File (Flat Data)
The CSV module in Python implements classes to read and write tabular data in CSV format.
Simple example for reading:
1 2 3 4 5 6 7 |
# Reading CSV content from a file import csv with open('C:/Users/ASUS/Documents/data.csv', newline='') as f: reader = csv.reader(f) for row in reader: print(row) |
Output in Python GUI:
Simple example for writing:
1 2 3 4 5 6 7 8 |
import csv with open('C:/Users/ASUS/Documents/employee_file.csv', mode='w') as employee_file: employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) employee_writer.writerow(['John Smith', 'Accounting', 'November']) employee_writer.writerow(['Erica Meyers', 'IT', 'March']) |
Output in Python GUI:
There is no expected output in the Python GUI, but, let’s see the result in Ms. Excel:
2.3. JSON File (Nested Data)
Python provides built-in JSON libraries to encode and decode JSON.
There are two basic formats for JSON data. Either in a string or the object datastructure. The object datastructure, in Python, consists of lists and dictionaries nested inside each other. The object datastructure allows one to use python methods (for lists and dictionaries) to add, list, search and remove elements from the datastructure. The String format is mainly used to pass the data into another program or load into a datastructure.
To load JSON back to a data structure, use the “loads” method. This method takes a string and turns it back into the json object datastructure:
1 2 3 |
import json print(json.loads(json_string)) |
To encode a data structure to JSON, use the “dumps” method. This method takes an object and returns a String:
1 2 3 4 |
import json json_string = json.dumps([1, 2, 3, "a", "b", "c"]) print(json_string) |
Output:
Python supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle).
We can use it exactly the same way:
1 2 3 4 |
import pickle pickled_string = pickle.dumps([1, 2, 3, "a", "b", "c"]) print(pickle.loads(pickled_string)) |
Output:
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.