use pickle dump & load data with file pickle.dump

简介:
pickle.dump 导出到文件
pickle.load 从文件读取
数据流格式为binary.
例子 :
[root@localhost ~]# vi test.py
import pickle
import sys

l_list = ['hello',['i',['am','digoal']]]
with open("/tmp/abc.txt", "wb") as f1:
  pickle.dump(l_list, f1)
with open("/tmp/abc.txt", "rb") as f2:
  new_list=pickle.load(f2)
print(new_list,file=sys.stdout)

输出
[root@localhost ~]# python test.py
['hello', ['i', ['am', 'digoal']]]

pickle的格式是binary的, 直接读取是一堆乱码.
[root@localhost ~]# cat /tmp/abc.txt 
?]q(Xhelloq]q(Xiq]q(XamqXdigoalqeee.

[其他]
1.
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Print objects to the text stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

2. 
The strip()  method removes unwanted whitespace from strings.

The file argument to the print() BIF controls where data is sent/saved.

The finally suite is always executed no matter what exceptions occur within a try/except statement.

An exception object is passed into the except suite and can be assigned to an identifier using the as keyword.

The str()  BIF can be used to access the stringed representation of any data object that supports the conversion.

The locals()  BIF returns a collection of variables within the current scope.

The in operator tests for membership.

The “+” operator concatenates two strings when used with strings but adds two numbers together when used with numbers.

The with statement automatically arranges to close all opened files, even when exceptions occur. The with statement uses the as keyword, too.

sys.stdout is what Python calls “standard output” and is available from the standard library’s sys module.

The standard library’s pickle module lets you easily and efficiently save and restore Python data objects to disk.

The pickle.dump()  function saves data to disk.

The pickle.load()  function restores data from disk.




[参考]
目录
相关文章
|
6月前
|
存储 新零售 安全
阿里云企业网盘收费标准:存储空间、用户数配置价格及功能使用全解析
阿里云盘企业版,500GB仅169元/年,支持协同办公、360°权限管理、智能文件检索、多重安全防护及极速传输,高性价比助力企业高效协作,适用于多行业数字化办公需求。
|
JSON 移动开发 JavaScript
多款顶级好用的 Vue 表单设计器测评推荐,可拖拽生成表单
Vue 前端开发中,表单组件是排在前三的高频使用的组件,如何快速构建表单,节省力气,避免重复造轮子呢,选择一款适合自己的前端表单设计器就非常重要了。本文介绍 4 款顶级好用的 Vue 表单设计器,其中最后一款卡拉云,是新一代低代码开发工具,不仅能自动生成各类表单,还可以拖拽生成其他常见的前端组件,一行代码连接前后端数据,可快速接入数据库/api。它是表单设计器的超集,可直接生成属于你的后台管理工具,无敌好用。
4419 0
多款顶级好用的 Vue 表单设计器测评推荐,可拖拽生成表单
|
10月前
|
监控 安全 Ubuntu
从零开始学安全:服务器被入侵后的自救指南
在信息爆炸时代,服务器安全至关重要。本文针对黑客入侵问题,从应急处理、系统恢复到安全加固全面解析。发现入侵时应冷静隔离服务器,保存日志证据,深入排查痕迹;随后通过重装系统、恢复数据、更改密码完成清理;最后加强防火墙、更新软件、部署检测系统等措施防止二次入侵。服务器安全是一场持久战,需时刻警惕、不断优化防护策略。
1367 1
|
网络安全 Python
Python:获取ssl证书信息和到期时间
Python:获取ssl证书信息和到期时间
1407 0
|
12月前
|
存储 数据采集 分布式计算
别光堆数据,架构才是大数据的灵魂!
别光堆数据,架构才是大数据的灵魂!
349 13
|
SQL 安全 数据库
ctfshow-WEB-web6
ctf.show WEB模块第6关是一个SQL注入漏洞,注入点是单引号字符型注入,并且过滤了空格,我们可以使用括号()或者注释/**/绕过
512 0
ctfshow-WEB-web6
|
数据采集 机器学习/深度学习 算法
这8张“数据分析”高清知识图谱,真的秒杀一切!
这8张“数据分析”高清知识图谱,真的秒杀一切!
这8张“数据分析”高清知识图谱,真的秒杀一切!
|
存储 机器学习/深度学习 编解码
阿里云服务器2核4G配置可选实例规格详解及优惠价格表(2023年更新)
阿里云服务器2核4G配置多少钱?可选实例规格有哪些?根据阿里云公布的2023年新版收费标准及活动价格来看,阿里云服务器2核4G最低活动价格是42.66元3个月,卷后632.56元/1年,轻量应用服务器2核4G354元1年。阿里云服务器2核4G配置可选的规格有18个,不同实例的价格有所不同,下面是2核4G配置可选实例规格详解及优惠价格表。
阿里云服务器2核4G配置可选实例规格详解及优惠价格表(2023年更新)
|
NoSQL 定位技术 C#
基于C#的ArcEngine二次开发43:GP工具--数据管理工具--Joins
基于C#的ArcEngine二次开发43:GP工具--数据管理工具--Joins
基于C#的ArcEngine二次开发43:GP工具--数据管理工具--Joins
|
SQL 开发框架 编解码
前端开发规范
前端开发规范
1469 0
前端开发规范

热门文章

最新文章