One, the JSON module
1. Definition
The process of converting dictionaries, lists, and so on into strings is serialization.
The operation has a limited data type, but can support all programming language operations.
2, why should there be serialization?
1. Persist the custom object in some form of storage.
2. Passing objects
3, is the program more maintenance
3. Four functions (Dumps,loads,dump,load)
Dumps loads
Import = ['aa'' b3']ret = json.dumps (LST) # Serialized print (ret) = json.loads (ret) # deserialization print (RET1)
Dump load
ImportJsonwith Open ('T1.txt', mode='W', encoding='Utf-8') as F:json.dump (LST, f)#Serialization of #dump is used to write the serialized string directly to the fileWith Open ('T1.txt', mode='R', encoding='Utf-8') as F:Print(Json.load (f))#deserialization #load can return a string from a file to
Limitations of the JSON module
1, when the JSON operation dictionary, key must be a string form.
2, the JSON format of the dictionary, the key cannot be a number, if it is a number, will be forcibly transferred to Str. And after deserialization, it is a string.
3, the JSON format string must be the form of double quotation marks, if the JSON format of the string of double quotes into single quotation marks, will be an error.
4. JSON serialization of Ganso is forced to be converted to a list, deserialized or a list, without returning the ancestor.
5, JSON support Ganso to do dictionary value value, do not support to do key.
6, Json.dump supports multiple dump write files, but does not support load. (multiple types appear on one line because they appear after writing.)
To dump multiple data into a file, you need to use dumps
7, for Chinese, if not processed, after serialization will become ASCII code type.
Therefore, when ensure_ascii = False, it can be a Chinese type.
8. Set cannot be Dumps/dump
Second, Pickel is a Python-specific module, not cross-language, but supported by a variety of data types.
1, dumps, loads (the result of Pickel.dumps is Bytesle type)
2. Dump load
If the file is operating, dump is required when WB mode is written, while load requires the RB mode to read out.
3, can be carried out multiple dump/load operation.
Third, shelve
1, when you write a file, the changes to the file is small, the number of times to read files, you can use shelve.
Serialization module JSON Pickel shelve