JSON & Pickle Before we learned to use the eval built-in method to turn a string into a Python object, the Eval method has limitations, for normal data types, Both Json.loads and Eval are available, but when a particular type is encountered, eval is not used, so the focus of eval is usually used to execute a string expression and return the value of the expression.
1234 |
import json x = "[null,true,false,1]" print ( eval (x)) print (json.loads(x)) |
What is serialization?
The process of changing an object (variable) from memory to a storage or transfer is called serialization, and in Python it is called pickling, which is also called serialization,marshalling,flattening in other languages, and so on.
After serialization, the serialized content can be written to disk or transferred over the network to another machine.
In turn, re-reading the variable contents from the serialized object into memory is called deserialization, i.e. unpickling.
Json
If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.
JSON represents objects that are standard JavaScript language objects, and JSON and Python have built-in data types that correspond to the following:
1234567891011121314151617 |
#----------------------------序列化
import
json
dic
=
{
‘name‘
:
‘alvin‘
,
‘age‘
:
23
,
‘sex‘
:
‘male‘
}
print
(
type
(dic))
#<class ‘dict‘>
j
=
json.dumps(dic)
print
(
type
(j))
#<class ‘str‘>
f
=
open
(
‘序列化对象‘
,
‘w‘
)
f.write(j)
#-------------------等价于json.dump(dic,f)
f.close()
#-----------------------------反序列化<br>
import
json
f
=
open
(
‘序列化对象‘
)
data
=
json.loads(f.read())
# 等价于data=json.load(f)
|
watch out.Pickle
1234567891011121314151617181920212223 |
##----------------------------序列化
import
pickle
dic
=
{
‘name‘
:
‘alvin‘
,
‘age‘
:
23
,
‘sex‘
:
‘male‘
} print
(
type
(dic))
#<class ‘dict‘>
j
=
pickle.dumps(dic)
print
(
type
(j))
#<class ‘bytes‘>
f
=
open
(
‘序列化对象_pickle‘
,
‘wb‘
)
#注意是w是写入str,wb是写入bytes,j是‘bytes‘
f.write(j)
#-------------------等价于pickle.dump(dic,f)
f.close()
#-------------------------反序列化
import
pickle
f
=
open
(
‘序列化对象_pickle‘
,
‘rb‘
)
data
=
pickle.loads(f.read())
# 等价于data=pickle.load(f)
print
(data[
‘age‘
])
|
The problem with Pickle is the same as for all other programming language-specific serialization problems, that is, it can only be used in Python, and may be incompatible with each other in Python, so it's okay to save only those unimportant data with pickle and not successfully deserialize it.
Shelve module
The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable; key must be a string, and the value can be a data type supported by Python
123456789101112 |
import
shelve
f
=
shelve.
open
(r
‘shelve.txt‘
)
# f[‘stu1_info‘]={‘name‘:‘alex‘,‘age‘:‘18‘}
# f[‘stu2_info‘]={‘name‘:‘alvin‘,‘age‘:‘20‘}
# f[‘school_info‘]={‘website‘:‘oldboyedu.com‘,‘city‘:‘beijing‘}
#
#
# f.close()
print
(f.get(
‘stu_info‘
)[
‘age‘
])
|
Python JSON & Pickle & Shelve Module