@@ -12,10 +12,150 @@ What is data serialization?
1212
1313Data serialization is the concept of converting structured data into a format
1414that allows it to be shared or stored in such a way that its original
15- structure to be recovered. In some cases, the secondary intention of data
15+ structure can be recovered or reconstructed . In some cases, the secondary intention of data
1616serialization is to minimize the size of the serialized data which then
1717minimizes disk space or bandwidth requirements.
1818
19+ ********************
20+ Flat vs. Nested data
21+ ********************
22+
23+ Before beginning to serialize data, it is important to identify or decide how the
24+ data needs to be structured during data serialization - flat or nested.
25+ The differences in the two styles are shown in the below examples.
26+
27+ Flat style:
28+
29+ .. code-block :: python
30+
31+ { " Type" : " A" , " field1" : " value1" , " field2" : " value2" , " field3" : " value3" }
32+
33+
34+ Nested style:
35+
36+ .. code-block :: python
37+
38+ {" A"
39+ { " field1" : " value1" , " field2" : " value2" , " field3" : " value3" } }
40+
41+
42+ For more reading on the two styles, please see the discussion on
43+ `Python mailing list <https://mail.python.org/pipermail/python-list/2010-October/590762.html >`__,
44+ `IETF mailing list <https://www.ietf.org/mail-archive/web/json/current/msg03739.html >`__ and
45+ `here <https://softwareengineering.stackexchange.com/questions/350623/flat-or-nested-json-for-hierarchal-data >`__.
46+
47+ ****************
48+ Serializing Text
49+ ****************
50+
51+ =======================
52+ Simple file (flat data)
53+ =======================
54+
55+ If the data to be serialized is located in a file and contains flat data, Python offers two methods to serialize data.
56+
57+ repr
58+ ----
59+
60+ The repr method in Python takes a single object parameter and returns a printable representation of the input
61+
62+ .. code-block :: python
63+
64+ # input as flat text
65+ a = { " Type" : " A" , " field1" : " value1" , " field2" : " value2" , " field3" : " value3" }
66+
67+ # the same input can also be read from a file
68+ a =
69+
70+ # returns a printable representation of the input;
71+ # the output can be written to a file as well
72+ print (repr (a))
73+
74+ # write content to files using repr
75+ with open (' /tmp/file.py' ) as f:f.write(repr (a))
76+
77+ ast.literal_eval
78+ ________________
79+
80+ The literal_eval method safely parses and evaluates an expression for a Python datatype.
81+ Supported data types are: strings, numbers, tuples, lists, dicts, booleans and None.
82+
83+ .. code-block :: python
84+
85+ with open (' /tmp/file.py' , ' r' ) as f: inp = ast.literal_eval(f.read())
86+
87+ ====================
88+ CSV file (flat data)
89+ ====================
90+
91+ The CSV module in Python implements classes to read and write tabular
92+ data in CSV format.
93+
94+ Simple example for reading:
95+
96+ .. code-block :: python
97+
98+ import csv
99+ with open (' /tmp/file.csv' , newline = ' ' ) as f:
100+ reader = csv.reader(f)
101+ for row in reader:
102+ print (row)
103+
104+ Simple example for writing:
105+
106+ .. code-block :: python
107+
108+ import csv
109+ with open (' /temp/file.csv' , ' w' , newline = ' ' ) as f:
110+ writer = csv.writer(f)
111+ writer.writerows(iterable)
112+
113+
114+ The module's contents, functions and examples can be found
115+ `here <https://docs.python.org/3/library/csv.html >`__.
116+
117+ ==================
118+ YAML (nested data)
119+ ==================
120+
121+ There are many third party modules to parse and read/write YAML file
122+ structures in Python. One such example is below.
123+
124+ .. code-block :: python
125+
126+ import yaml
127+ with open (' /tmp/file.yaml' , ' r' , newline = ' ' ) as f:
128+ try :
129+ print (yaml.load(f))
130+ except yaml.YAMLError as ymlexcp:
131+ print (ymlexcp)
132+
133+ Documentation on the third party module can be found
134+ `here <https://pyyaml.org/wiki/PyYAMLDocumentation >`__.
135+
136+ =======================
137+ JSON file (nested data)
138+ =======================
139+
140+ Python's JSON module can be used to read and write JSON files.
141+ Example code is below.
142+
143+ Reading:
144+
145+ .. code-block :: python
146+
147+ import json
148+ with open (' /tmp/file.json' , ' r' ) as f:
149+ data = json.dump(f)
150+
151+ Writing:
152+
153+ .. code-block :: python
154+
155+ import json
156+ with open (' /tmp/file.json' , ' w' ) as f:
157+ json.dump(data, f, sort_keys = True )
158+
19159
20160******
21161Pickle
0 commit comments