Created for
import pickle
# let's serialize a simple dict
prices = { 'apples': 2.50, 'oranges': 1.90, 'bananas': 2.40 }
#convert the object to a serialized string
serialized_prices = pickle.dumps( prices )
print(serialized_prices)
#de-serialize (unpickle) an object
received_prices = pickle.loads( serialized_prices )
print(received_prices)
JSON is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value)JSON @wikipedia
examples of JSON data: Examples @JSON Schema
{
"title": "Person",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
json
modulejson.loads() function
json.loads()
example
import json
from operator import itemgetter
json_str = """
[
{
"name": "apple",
"price": 1.80
},
{
"name": "orange",
"price": 2.10
},
{
"name": "bananas",
"price": 1.60
}
]
"""
#read json from string
json_data = json.loads(json_str)
# print the list sorted by "price"
for i in sorted(json_data,key=itemgetter("price")):
print(i)
json.load() function
json.load()
sample file
[
{
"name": "apple",
"price": 1.80
},
{
"name": "orange",
"price": 2.10
},
{
"name": "bananas",
"price": 1.60
}
]
json.load()
example
import json
from operator import itemgetter
json_file = "sample.json"
#read json from file
with open(json_file) as f:
json_data = json.load(f)
for i in sorted(json_data,key=itemgetter("price")):
print(i)
json.dump() function
json.dumps() function
dump()
, but serialize to stringjson.dumps()
- list example
import json
mylist = [1,2,3]
matrix = [
[1,2,3],
[4,5,6],
[7,8,9],
]
print('List :', json.dumps(mylist))
print('Matrix :', json.dumps(matrix))
json.dumps()
- indented list example
import json
mylist = [1,2,3]
matrix = [
[1,2,3],
[4,5,6],
[7,8,9],
]
print('List :', json.dumps(mylist,indent=2))
print('Matrix :', json.dumps(matrix,indent=2))
Symbol,Price,Date,Time,Change,Volume
"AA",39.48,"6/11/2007","9:36am",-0.18,181800
"AIG",71.38,"6/11/2007","9:36am",-0.15,195500
"AXP",62.58,"6/11/2007","9:36am",-0.46,935000
"BA",98.31,"6/11/2007","9:36am",+0.12,104800
"C",53.08,"6/11/2007","9:36am",-0.25,360900
"CAT",78.29,"6/11/2007","9:36am",-0.23,225400
import csv
with open('sample_data.csv') as f:
f_csv = csv.reader(f)
headers = next(f_csv)
for row in sorted(f_csv, key=lambda a:a[0]):
print(row)
(XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable.XML @wikipedia
Processing XML in Python:
<data>
<items>
<item atr1="value1">item1 data</item>
<item atr2="value2">item2 data</item>
</items>
</data>
import xml.etree.ElementTree as ET
tree = ET.parse('sample_data.xml')
root = tree.getroot()
# print items attributes
print('\nAll attributes:')
for elem in root:
for subelem in elem:
print(subelem.attrib)
# print items data
print('\nAll item data:')
for elem in root:
for subelem in elem:
print(subelem.text)
These slides are based on
customised version of
framework