@@ -74,8 +74,9 @@ The repr method in Python takes a single object parameter and returns a printabl
74
74
# write content to files using repr
75
75
with open (' /tmp/file.py' )as f:f.write(repr (a))
76
76
77
+
77
78
ast.literal_eval
78
- ________________
79
+ ----------------
79
80
80
81
The literal_eval method safely parses and evaluates an expression for a Python datatype.
81
82
Supported data types are: strings, numbers, tuples, lists, dicts, booleans and None.
@@ -95,6 +96,7 @@ Simple example for reading:
95
96
96
97
..code-block ::python
97
98
99
+ # Reading CSV content from a file
98
100
import csv
99
101
with open (' /tmp/file.csv' ,newline = ' ' )as f:
100
102
reader= csv.reader(f)
@@ -105,6 +107,7 @@ Simple example for writing:
105
107
106
108
..code-block ::python
107
109
110
+ # Writing CSV content to a file
108
111
import csv
109
112
with open (' /temp/file.csv' ,' w' ,newline = ' ' )as f:
110
113
writer= csv.writer(f)
@@ -123,6 +126,7 @@ structures in Python. One such example is below.
123
126
124
127
..code-block ::python
125
128
129
+ # Reading YAML content from a file using the load method
126
130
import yaml
127
131
with open (' /tmp/file.yaml' ,' r' ,newline = ' ' )as f:
128
132
try :
@@ -144,22 +148,66 @@ Reading:
144
148
145
149
..code-block ::python
146
150
151
+ # Reading JSON content from a file
147
152
import json
148
153
with open (' /tmp/file.json' ,' r' )as f:
149
- data= json.dump (f)
154
+ data= json.load (f)
150
155
151
156
Writing:
152
157
153
158
..code-block ::python
154
159
160
+ # writing JSON content to a file using the dump method
155
161
import json
156
162
with open (' /tmp/file.json' ,' w' )as f:
157
163
json.dump(data, f,sort_keys = True )
158
164
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.ElementTreeas 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
+ =======================
159
191
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 numpyas 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
+ ====================
163
211
164
212
The native data serialization module for Python is called `Pickle
165
213
<https://docs.python.org/2/library/pickle.html> `_.