Site icon Python GUI

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

blogbanner3 7

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 Demo01dproj

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:

[crayon-66224fd66158b598030530/]

Nested style:

[crayon-66224fd661594775351931/]

 

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:

[crayon-66224fd661595716480869/]

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:

[crayon-66224fd661597043394352/]

Output in Python GUI:

Reading CSV File in Python GUI

Simple example for writing:

[crayon-66224fd661598543624076/]

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:

[crayon-66224fd66159c911400650/]

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

[crayon-66224fd66159d022004585/]

Output:

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:

[crayon-66224fd66159f896099659/]

Output:

Python Pickle in Python GUI

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

Exit mobile version