Python: serialization and data persistence, python serialization
Data Persistence methods include:
1. Normal file writing without format: write data directly to the file
2. Normal serialized write: json, pickle
3. DBM mode: shelve, dbm
Related content:
Start Time:
Json: Introduction:
Write the data in plaintext to the file according to the specified format (for example, the format is dictionary, and the file is dictionary). The data type is bytes. For example, "Chinese" is converted to Unicode encoding.
Usage:
- First, import the module import json
- Serialization:
- Json. dump (serialized object, file object)
- Json. dumps (serialized object), the return value is a string, you need to manually write this string into the file
Print ("------ json serialization --------") import jsonimport timeinfo = {'date': time. localtime (), 'name': 'Chinese'} f = open ("test.txt", "w") print ("--------- dump ---------") # json. dump (info, f) # f. close () print ("--------- dumps, ---------") f. write (json. dumps (info) f. close ()
- Deserialization:
For multiple dump \ dumps, how to load \ loads:
- You need to manually divide the data during dump.
Print ("------ json serialization --------") import jsonimport timeinfo = {'date': time. localtime (), 'name': 'Chinese' # 'func': hello # Note: json deserialization functions} info2 = ['1', 2, 3, 4] f = open ("test.txt", "w") print ("--------- dumps, ---------") # Use '\ n' to distinguish two data copies. write (json. dumps (info) + "\ n") f. write (json. dumps (info2) + "\ n") f. close ()
import jsonwith open("test.txt") as f: a=json.loads(f.readline()) b=json.loads(f.readline()) print(a,b)
Pickle: Introduction:
- Conversion between Python data types and Python-specific binary formats
- The protocol parameter specifies the protocol version for serialization. By default, pikkle is used to serialize data by bytes. The file opening method must be in binary format.
Usage:
- First import the module import pickle
- Serialization:
- Pickle. dump (serialized object, file object)
- Pickle. dumps (serialized object), the return value is a string, you need to manually write this string into the file
Import pickleinfo = {'name': '1', 'age': 2,} f = open ("test2.txt", "wb") pickle. dump (info, f) # serialization method 1 # f. write (pickle. dumps (info) # serialization method 2f. close ()
- Deserialization:
Shelve: Introduction:
- It is used to persist Python data to a disk and perform operations similar to dict.
Usage:
- First import the module import
- Shelve open a file: shelve file object = shelve. open (file name)
- Write: shelve file object [key] = value
- Read: shelve file object. get (key)
Import shelve, timed = shelve. open ('shelve _ test') # open a file print ("---------- write ----------") info = {"name": 'lilei', "sex ": "man"} name = ["autuman", "zhangsan ", "lisi"] d ["teacher"] = named ["student"] = infod ["date"] = time. ctime () print ("-------- read ------------") print (d. get ("teacher") print (d. get ("student") print (d. get ("date") d. close ()
Shelve can easily serialize Custom Data Types and functions:
Import shelve, timeclass A: def hello (self): print ("123") d = shelve. open ('shelve _ test') # open a file print ("---------- write ----------") d ['class'] = Aprint ("-------- read ------------") a = d. get ('class') (). hello () d. close ()
Dbm: Introduction:
- Dbm is very similar to shelve, but the key and value of dbm must be of the string type.
- Dbm data written by default is bytes, and all strings are serialized into bytes
Usage:
- First, import the module imort dbm [note that there are many different dbm which can be selected for use. The default value is used here]
- Open File: dbm object = dbm. open (file name, open mode)
- Write: dbm object [key] = value
- Read: dbm object [key]
Import dbmdb = dbm. open ("test.txt", "c") print ("write ". center (50, '-') db ["name"] = "1111111111112" db ["name2"] = "2222222222222" print ("read ". center (50, '-') print (db ["name"]) print (db ["name2"]) db. close ()