Sequencing module -json

1. What is serialized ------- converting the original dictionaries, lists and other content into a string of process is called serialization

2. The purpose of the sequence

  1. custom object so some form of persistent storage

  2. The object is passed from one place to another

  3. To make the program more maintainable

json

  Json module provides four functions: dumps, loads, dump, load

# JSON provides four functions dumps, loads, the dump, Load 
Import JSON 
DIC = { ' . 1 ' : ' A ' , ' 2 ' : ' B ' } 
RET = json.dumps (DIC)
 Print (RET) 

DIC1 = JSON. loads (RET)
 Print (DIC1)
import json
dic = {'1':'a','2':'b'}
f = open('01','w',encoding='utf-8')
json.dump(dic,f)

f = open('01','r',encoding='utf-8')
ret = json.load(f)

import json
l = [{'1':'a','2':'b'},{'3':'s','4':'q'},{'5':'w','6':'e'}]
f = open('01','w')
for i in l:
    ret = json.dumps(i)
    f.write(ret+'\n')
f.close()
import json
f = open('01','r',encoding='utf-8')
for line in f:
    ret = json.loads(line)
    print(ret)
f.close()

 

Guess you like

Origin www.cnblogs.com/li33232/p/11482606.html