json序列化

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Random_lee
#pickle只能在python中使用,可以序列化函数的内存地址
#json可以在多语言中使用,但是只能做简单的序列化


# import json
import pickle

def sayhai(name):
print('hello',name)


info={
'name':'randomlee',
'age':'24',
'sex':'man',
'func':sayhai
}

# f=open('test.text','w')
# # print(json.dumps(info))
# f.write(json.dumps(info))
# f.close()

f=open('test.text','bw')
print(pickle.dumps(info))
f.write(pickle.dumps(info))
f.close()


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Random_lee

import pickle

def sayhai(name):
print('hello',name)


info={
'name':'randomlee',
'age':'24',
'sex':'man',
'func':sayhai
}

f=open('test1.text','wb')
pickle.dump(info,f) #f.write(pickle.dumps(info))
f.close()



#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Random_lee

# import pickle
import json


# def sayhai(name):
# print('hello',name)


info={
'name':'randomlee',
'age':'24',
'sex':'man',
'func':'sayhai'
}

f=open('test3.text','w')
f.write(json.dumps(info))

# info['age']=22
# f.write(json.dumps(info))

f.close()

原文地址:https://www.cnblogs.com/randomlee/p/8686192.html