序列化 json 和 pickle 模块

什么是序列化 -- json

lit = [1,22,3,3,45] # [1,22,3,3,45]

s_lst = str(lit)

print(s_lst,type(s_lst))

print(list(s_lst))

print(eval(s_lst)) # 禁止使用

1.dumps loads(用来操作数据类型的(对象))

将字符串类型的字典转换成字典类型

将字典类型转换成字符串类型

import json

lst=[1,2,34,45,6,]

s=json.dumps(lst)

print(s)

s1=json.loads(s)

print(s1)

2.dump load(用来操作文件的)

操作文件中一行数据时

import json

f=open('json_filf.json','w',encoding='utf-8')

dic=

json.dump(dic,f) # 只能写一个文件 不能创建多个字典 1.问题无法转换成中1文

f.close()

f1=open('json_filf.json')

dic2=json.load(f1)

f1.close()

print(dic2)

json序列化存储多个数据到同一个文件中

对于json 存储多个数据到文件中

import json

同时写多个内容 进行序列化

lst = [1,2,3,4,56,]

f = open("info","w",encoding="utf-8")

f.write(json.dumps(lst) + "\n")

f.write(json.dumps(lst) + "\n")

f.write(json.dumps(lst) + "\n")

f.write(json.dumps(lst) + "\n")

f.write(json.dumps(lst) + "\n")

f.close()

f = open("info",encoding='utf-8')

for line in f:

print(json.loads(line))

dump: 将对象转换(序列化)成字符串,写入文件

load: 将文件中字符串转换(反序列)成对象

loads:对多个文件操作时可以使用加for循环

pickle模块 常用来反序列化

dumps、loads

import pickle

    # lst = [12,3,4,5,768]
    # t_list = pickle.dumps(lst) # 转换成类似字节
    # print(t_list)
    # print(pickle.loads(t_list)[-1])

    # dic = {"user":"郭宝元"}
    # t_list = pickle.dumps(dic) # 转换成类似字节
    # print(t_list)
    # print(pickle.loads(t_list))

dump load 对文件进行操作

import pickle

dic =

dic =

pickle.dump(dic,open("info","wb"))

print(pickle.load(open("info","rb")))

import pickle

dic =

pickle.dump(dic,open("info","ab"))

import pickle

dic =

f = open("info","wb")

s = "\n".encode("utf-8")

f.write(pickle.dumps(dic)+ s)

f.write(pickle.dumps(dic)+ s)

f.write(pickle.dumps(dic)+ s)

f.close()

f1 = open("info","rb")

for i in f1:

print(pickle.loads(i))

可以对函数进行操作

1.

# def func():

# print(111)

# import pickle

# fun = pickle.dumps(func)

# print(fun)

# fun = pickle.dumps(func)

# print(fun)

# pickle.loads(fun)()

2.

import pickle

def func():

print(666)

ret = pickle.dumps(func)

print(ret,type(ret)) # b'\x80\x03c__main__\nfunc\nq\x00.' <class 'bytes'>

f1 = pickle.loads(ret) # f1得到 func函数的内存地址

f1() # 执行func函数

ret = pickle.dumps(func)

print(ret,type(ret)) # b'\x80\x03c__main__\nfunc\nq\x00.' <class 'bytes'>

f1 = pickle.loads(ret) # f1得到 func函数的内存地址

f1() # 执行func函数

pickle 序列化 - nb(python所有对象进行转换)

python自带的(只有python可以用)

1. dumps loads

    # import pickle

    # lst = [12,3,4,5,768]
    # t_list = pickle.dumps(lst) # 转换成类似字节
    # print(t_list)
    # print(pickle.loads(t_list)[-1])

    # dic = {"user":"郭宝元"}
    # t_list = pickle.dumps(dic) # 转换成类似字节
    # print(t_list)
    # print(pickle.loads(t_list))

    # def func():
    #     print(111)

    # import json
    # fun = json.dumps(func)
    # print(fun)

    # fun = pickle.dumps(func)
    # print(fun)
    # pickle.loads(fun)()

2. dump load

import pickle

dic =

dic =

pickle.dump(dic,open("info","wb"))

print(pickle.load(open("info","rb")))

import pickle

dic =

pickle.dump(dic,open("info","ab"))

import pickle

dic =

f = open("info","wb")

s = "\n".encode("utf-8")

f.write(pickle.dumps(dic)+ s)

f.write(pickle.dumps(dic)+ s)

f.write(pickle.dumps(dic)+ s)

f.close()

f1 = open("info","rb")

for i in f1:

print(pickle.loads(i))

import pickle

dic =

f = open("info","wb")

s = "\n".encode("utf-8")

f.write(pickle.dumps(dic)+ s)

f.write(pickle.dumps(dic)+ s)

f.write(pickle.dumps(dic)+ s)

f.close()

f1 = open("info","rb")

for i in f1:

print(pickle.loads(i))

推荐使用json

json是各种语言通用的

pickle(python私有)

asdfasd

posted on 2019-07-26 21:50  西雅图13045195770  阅读(98)  评论(0编辑  收藏  举报