python常用模块学习3

# # dic='{"name":"tang"}'
# # f=open("hello",'w')
# # f.write(dic)
# # f.close()
# #
# #
# #
# # f_read=open("hello",'r')
# # data=f_read.readline()
# # print(type(data))
# #
# # print(eval(data)['name'])
#
#
# import json-----
#
# # 规范:
#json模块--------
#
# dic = {'name': 'alex'}  # --->{"name":"alex"}--->'{"name":"alex"}'
# # dic_str=json.dumps(dic)  #将数据类型转换为json格式
# f = open("new_hello", "w")
# # f.write(dic_str)
# json.dump(dic, f)
#
# f.close()
#
# # print(type(dic_str))
# # print(dic_str)
#
# i = 8  # ---->'8'
# s = 'hello'
# print(json.dumps(i))
# print(json.dumps(s))
#
# # a=[1,3,4,5]---->"[]"
# # print(json.dumps(a))
#
#
# f_read = open("new_hello", "r")
# # print(f_read.read())
# data = json.loads(f_read.read())
# print(data['name'])
# print(type(data))



#如果字符串遵循json.dump符合json语法,也可以使用json.loads方法,无序成对使用
# import json
# with open("json_test","r") as f:
#     data=f.read()
#     print(data)
#     data=json.loads(data)
#     print(data['name'])




#---------pickle模块--------
import pickle
# dic={"name":"alex","age":18}
# print(type(dic))
# j=pickle.dumps(dic)
# print(type(j))#class "bytes"


# f=open("序列化对象pickle","wb")#注意w是写入str,wb是写入bytes,j是bytes
# f.write(j)#-------等价于pickle.dump(dic,f)
#
# f.close()


#------------返序列化
f=open("序列化对象pickle","rb")

data=pickle.load(f)
print(data['age'])
f.close()

 

posted @ 2019-07-18 11:15  coldplaycode  阅读(183)  评论(0编辑  收藏  举报

coldplaycode