#json 模式

1、dumps、loads  方法 针对内存

dic = {'k1':'v1'}
#转换成json
import json
str_d = json.dumps(dic) #序列化
dic_d = json.loads(str_d)#反序列化
#可序列化的类型 数字 字符串 列表 字典

2、dump 、load 方法  针对文件

dic = {1:'a',2:'b'}
f = open('fff','w',encoding = 'utf-8')
json.dump(dic,f,ensure_ascii=False) #序列化 按照正常的编码格式,显示中文
f.close()
f = open('fff','w',encoding = 'utf-8')
ret = json.load() #反序列化
print(type(res),res)
f.close()

#pickle模式

#可以分布的 dump load 操作文件需要使用 'wb' 模式
import pickle
dic = {'k1':'v1','k2':'v2','k3':'v3'}
str_dic = pickle.dumps(dic)
print(str_dic) #一串二进制内容 dic2 = pickle.loads(str_dic)
print(dic2) #字典 import time
struct_time1 = time.localtime(1000000000)
struct_time2 = time.localtime(2000000000)
f = open('pickle_file','wb')
pickle.dump(struct_time1,f)
pickle.dump(struct_time2,f)
f.close()
f = open('pickle_file','rb')
struct_time1 = pickle.load(f)
struct_time2 = pickle.load(f)
print(struct_time1.tm_year)
print(struct_time2.tm_year)
f.close()

#shelve

open方法

import shelve
f = shelve.open('shelve_file')
f['key'] = {'int':10, 'float':9.5, 'string':'Sample data'} #直接对文件句柄操作,就可以存入数据
f.close()
import shelve
f1 = shelve.open('shelve_file')
existing = f1['key'] #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错
f1.close()
print(existing) import shelve
f = shelve.open('shelve_file', flag='r')
existing = f['key']
print(existing) f.close() f = shelve.open('shelve_file', flag='r')
existing2 = f['key']
f.close()
print(existing2) import shelve
f1 = shelve.open('shelve_file')
print(f1['key'])
f1['key']['new_value'] = 'this was not here before'
f1.close() f2 = shelve.open('shelve_file', writeback=True)
print(f2['key'])
f2['key']['new_value'] = 'this was not here before'
f2.close()

Python——序列化模块的更多相关文章

  1. python序列化模块 json&&pickle&&shelve

    #序列化模块 #what #什么叫序列化--将原本的字典.列表等内容转换成一个字符串的过程叫做序列化. #why #序列化的目的 ##1.以某种存储形式使自定义对象持久化 ##2.将对象从一个地方传递 ...

  2. python序列化模块json和pickle

    序列化相关 1. json 应用场景: json模块主要用于处理json格式的数据,可以将json格式的数据转化为python的字典,便于python处理,同时也可以将python的字典或列表等对象转 ...

  3. python 序列化模块之 json 和 pickle

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,支持不同程序之间的数据转换.但是只能转换简单的类型如:(列表.字典.字符串. ...

  4. python序列化模块

    什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 序列化的目的 1.以某种存储形式使自定义对象持久化: 2.将对象从一个地方传递到另一个地方. 3.使程序更具维护性.   ...

  5. Python序列化模块pickle和json使用和区别

    这是用于序列化的两个模块: • json: 用于字符串和python数据类型间进行转换 • pickle: 用于python特有的类型和python的数据类型间进行转换 Json模块提供了四个功能:d ...

  6. Python 序列化模块(json,pickle,shelve)

    json模块 JSON (JavaScript Object Notation):是一个轻量级的数据交换格式模块,受javascript对象文本语法启发,但不属于JavaScript的子集. 常用方法 ...

  7. python序列化模块的速度比较

    # -*- coding: utf-8 -*- # @Time : 2019-04-01 17:41 # @Author : cxa # @File : dictest.py # @Software: ...

  8. Python序列化模块-Pickel写入和读取文件

    利用pickle 存储和读取文件 1.存储文件: #引入所需包,将列表元素存入data2的文件里面 import pickle mylist2 ={'1','nihao','之后','我们',1,2, ...

  9. 铁乐学python_day25_序列化模块

    铁乐学python_day25_序列化模块 部份内容摘自博客http://www.cnblogs.com/Eva-J/ 回顾内置方法: __len__ len(obj)的结果依赖于obj.__len_ ...

随机推荐

  1. Spring Cloud Eureka 常用配置及说明

    配置参数 默认值 说明 服务注册中心配置 Bean类:org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean eu ...

  2. html中节点类型

    常用的节点有元素节点.属性节点.文本节点.注释节点.文档节点 来看例子: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition ...

  3. [ArcGIS API for JavaScript 4.8] Sample Code-Get Started-widgets简介

    [官方文档:https://developers.arcgis.com/javascript/latest/sample-code/intro-widgets/index.html] 一.Intro ...

  4. 如何在vue单页应用中使用百度地图

    作为一名开发人员,每次接到开发任务,我们首先应该先分析需求,然后再思考技术方案和解决方案.三思而后行,这是一个好的习惯. 需求:本项目是采用vue组件化开发的单页应用项目,现需要在项目中引入百度的地图 ...

  5. AndroidTV端的requestFocus()问题

    每次开机盒子或者电视的时候,发现给某些控件设置请求焦点 requestFocus 会失效 最终的解决办法就是延时请求 view.postDelayed(new Runnable() { @Overri ...

  6. php+ajax实现登录按钮加载loading效果

    php+ajax实现登录按钮加载loading效果,一个提高用户体验,二个避免重复提交表单,ajax判断加载是否完成. 登录表单 <form onsubmit="return chec ...

  7. C++ 重点关键字

    const 四种用法 1.修饰变量起到限定只读作用: void func(const int a, const string str) {...} const int* func(...) {...} ...

  8. windows/Linux下的程序员文档浏览工具

    Dash + Alfred https://www.jianshu.com/p/77d2bf8df81f 对于程序员来说,查看api文档是非常频繁,经常窗口之间切换非常麻烦,mac下就有一个查文档的神 ...

  9. Linix基本命令

    基本命令关机:shutdown -h halt init 0 poweroff重启:shutdown -r reboot init 6pwd:查看工作目录ls:查看指定目录的内容-l:列表显示-a:显 ...

  10. 在Visual Studio 2017上配置Glut

    在Visual Studio 2017上配置Glut 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 在Visual Studio 2017上配置并使用 ...