首页 > 代码库 > python3 jason & pickle

python3 jason & pickle

#Author by Andy
#_*_ coding:utf-8 _*_
import json
import pickle
def sayhello(name):
print(‘Hello %s‘ %name)
return ‘Hello %s‘ %name

info={
‘name‘:‘Andy‘,
‘age‘:20,
‘job‘:‘doctor‘,

}

info1={
‘name‘:‘Andy‘,
‘age‘:20,
‘job‘:‘doctor‘,
‘func‘:sayhello

}
# print(type(info))
f=open(‘E:\my python study\\files\json & pickle.txt‘,‘rb‘)
# json.dumps(info)#将字典转为字符串(序列化)
# json.dump(info,f)#=f.write(json.dumps(info))
# json.load(f))#=json.loads(f.read())

# json.loads((json.dumps(info)))#将字符串转为字典
#Json主要用于不通语言之间的数据交互,支持字典、列表、字符串、元组、集合
# p=pickle.dumps(info1)#将字典转为二进制
# pickle.dump(info1,f)
#一个文件只能dump一次,load一次
# print(pickle.loads(p))#将二进制转为字典
def sayhello(name):
print(‘Hello %s‘ %name)
return ‘Hello %s‘ %name
print(pickle.load(f)[‘func‘])

python3 jason & pickle