DelphiLearn PythonPythonPython GUIRAD Studio

Learn To Build A High-Performance Data Serialization Pipeline With A Python Windows GUI


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.

Open Demo01.dproj

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:

Nested style:


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:


The repr method in Python takes a single object parameter and returns a printable representation of the input:

Output in Python GUI:

repr 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:

Output in Python GUI:

Reading CSV File in Python GUI.

Simple example for writing:

Output in Python GUI:

Writing CSV File in Python GUI.

There is no expected output in the Python GUI, but, let’s see the result in Ms. Excel:

Writing CSV File using Python GUI.


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:

To encode a data structure to JSON, use the “dumps” method. This method takes an object and returns a String:


Encode JSON in Python GUI.

Python supports a Python proprietary data serialization method called pickle (and a faster alternative called cPickle).

We can use it exactly the same way:


Python Pickle in Python GUI.

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Related posts
DelphiDelphiVCLPythonPython GUIWindows

Create A Drawing Space Using DelphiVCL.Canvas

Learn PythonPythonPython GUIVideos

Learn Python With Transitioning From Developing Software to Developing People by Matthew Knapp Bachman (PyCon 2020) Video

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

Decipher the DelphiVCL.Application

DelphiDelphiVCLLearn PythonPythonPython GUIWindows

Create a Very Basic Empty Form using DelphiVCL.Form

Leave a Reply

Your email address will not be published. Required fields are marked *