python json,pickle,shelve

'' ' 
Json:
for data exchange between different programs
dumps loads: json format handler
dump load: mainly used to read and write file functions json
pickle:
mainly used to read and write functions json file, to read and store all the word section form. Functions and almost json
The shelve:
The shelve module is simpler than pickle module, open only a function that returns a dictionary-like objects, readable and writable
'' '
file_path =' json.txt '
DN = {' baidu ':' www.baidu .com ',' QQ ':' www.qq.com ',' 360 ':' www.360.cn '}
name = [' mayun ',' mahuateng ',' liyanhong ']
Import JSON
DEF json_dump ():
Open with (file_path, 'W') AS f_write, \
Open (file_path, 'R & lt') AS f_read:
The json.dump (DN, f_write)
f_write.close ()
Data = JSON.


with open(file_path,'w') as f_write,\
open(file_path,'r') as f_read:
f_write.write(json.dumps(dn))
f_write.close()
data = f_read.read()
dicts = json.loads(data)
print(dicts)

import pickle
def pickle_dump():
with open(file_path,'wb') as f_write,\
open(file_path,'rb') as f_read:
pickle.dump(dn,f_write) ##写一个对象
pickle.dump(name,f_write) ##再写一个对象
f_write.close()

data=pickle.load(f_read)
print(data)
data = pickle.load(f_read)
print(data)

import shelve
def shelve_test():
f = shelve.open(file_path)
f['baidu'] = 'www.baidu.com'
f['qq'] = 'www.qq.com'
f['360'] = 'www.360.cn'
f.close()
f = shelve.open(file_path)
print(dict(f))
if __name__ == '__main__':
# json_dump()
# json_dumps()
# pickle_dump()
shelve_test()

Guess you like

Origin www.cnblogs.com/lides/p/11116454.html