Skip to content

Commit fd2a8f3

Browse files
author
Harish Kesava Rao
committed
Final commit
1 parent 8a6b1c7 commit fd2a8f3

File tree

1 file changed

+53
-5
lines changed

1 file changed

+53
-5
lines changed

docs/scenarios/serialization.rst

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ The repr method in Python takes a single object parameter and returns a printabl
7474
# write content to files using repr
7575
with open('/tmp/file.py') as f:f.write(repr(a))
7676
77+
7778
ast.literal_eval
78-
________________
79+
----------------
7980

8081
The literal_eval method safely parses and evaluates an expression for a Python datatype.
8182
Supported data types are: strings, numbers, tuples, lists, dicts, booleans and None.
@@ -95,6 +96,7 @@ Simple example for reading:
9596

9697
.. code-block:: python
9798
99+
# Reading CSV content from a file
98100
import csv
99101
with open('/tmp/file.csv', newline='') as f:
100102
reader = csv.reader(f)
@@ -105,6 +107,7 @@ Simple example for writing:
105107

106108
.. code-block:: python
107109
110+
# Writing CSV content to a file
108111
import csv
109112
with open('/temp/file.csv', 'w', newline='') as f:
110113
writer = csv.writer(f)
@@ -123,6 +126,7 @@ structures in Python. One such example is below.
123126

124127
.. code-block:: python
125128
129+
# Reading YAML content from a file using the load method
126130
import yaml
127131
with open('/tmp/file.yaml', 'r', newline='') as f:
128132
try:
@@ -144,22 +148,66 @@ Reading:
144148

145149
.. code-block:: python
146150
151+
# Reading JSON content from a file
147152
import json
148153
with open('/tmp/file.json', 'r') as f:
149-
data = json.dump(f)
154+
data = json.load(f)
150155
151156
Writing:
152157

153158
.. code-block:: python
154159
160+
# writing JSON content to a file using the dump method
155161
import json
156162
with open('/tmp/file.json', 'w') as f:
157163
json.dump(data, f, sort_keys=True)
158164
165+
=================
166+
XML (nested data)
167+
=================
168+
169+
XML parsing in Python is possible using the `xml` package.
170+
171+
Example:
172+
173+
.. code-block:: python
174+
175+
# reading XML content from a file
176+
import xml.etree.ElementTree as ET
177+
tree = ET.parse('country_data.xml')
178+
root = tree.getroot()
179+
180+
More documentation on using the `xml.dom` and `xml.sax` packages can be found
181+
`here <https://docs.python.org/3/library/xml.html>`__.
182+
183+
184+
*******
185+
Binary
186+
*******
187+
188+
=======================
189+
Numpy Array (flat data)
190+
=======================
159191

160-
******
161-
Pickle
162-
******
192+
Python's Numpy array can be used to serialize and deserialize data to and from byte representation.
193+
194+
Example:
195+
196+
.. code-block:: python
197+
198+
import numpy as np
199+
200+
# Converting Numpy array to byte format
201+
byte_output = np.array([ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]).tobytes()
202+
203+
# Converting byte format back to Numpy array
204+
array_format = np.frombuffer(byte_output)
205+
206+
207+
208+
====================
209+
Pickle (nested data)
210+
====================
163211

164212
The native data serialization module for Python is called `Pickle
165213
<https://docs.python.org/2/library/pickle.html>`_.

0 commit comments

Comments
 (0)