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.




[参考]
目录
相关文章
|
5月前
|
监控 安全 Ubuntu
从零开始学安全:服务器被入侵后的自救指南
在信息爆炸时代,服务器安全至关重要。本文针对黑客入侵问题,从应急处理、系统恢复到安全加固全面解析。发现入侵时应冷静隔离服务器,保存日志证据,深入排查痕迹;随后通过重装系统、恢复数据、更改密码完成清理;最后加强防火墙、更新软件、部署检测系统等措施防止二次入侵。服务器安全是一场持久战,需时刻警惕、不断优化防护策略。
673 1
|
1月前
|
存储 新零售 安全
阿里云企业网盘收费标准:存储空间、用户数配置价格及功能使用全解析
阿里云盘企业版,500GB仅169元/年,支持协同办公、360°权限管理、智能文件检索、多重安全防护及极速传输,高性价比助力企业高效协作,适用于多行业数字化办公需求。
|
JSON 移动开发 JavaScript
多款顶级好用的 Vue 表单设计器测评推荐,可拖拽生成表单
Vue 前端开发中,表单组件是排在前三的高频使用的组件,如何快速构建表单,节省力气,避免重复造轮子呢,选择一款适合自己的前端表单设计器就非常重要了。本文介绍 4 款顶级好用的 Vue 表单设计器,其中最后一款卡拉云,是新一代低代码开发工具,不仅能自动生成各类表单,还可以拖拽生成其他常见的前端组件,一行代码连接前后端数据,可快速接入数据库/api。它是表单设计器的超集,可直接生成属于你的后台管理工具,无敌好用。
4235 0
多款顶级好用的 Vue 表单设计器测评推荐,可拖拽生成表单
|
7月前
|
安全 算法 数据建模
HTTPS证书类型和品牌一览
HTTPS证书(SSL证书)是保障网站数据传输安全与身份可信认证的重要工具,适用于电商、企业官网等各类平台。证书主要分为DV(域名验证)、OV(企业验证)、EV(扩展验证)三种安全级别,以及单域名、通配符、多域名等不同覆盖类型。品牌方面,既有高性价比的国产锐安信、CFCA,也有国际知名的Sectigo、Digicert。
|
网络安全 Python
Python:获取ssl证书信息和到期时间
Python:获取ssl证书信息和到期时间
1276 0
|
网络协议 网络架构
【第三期】计算机网络常识/网络分层模型与数据包封装传输过程
【第三期】计算机网络常识/网络分层模型与数据包封装传输过程
423 0
|
SQL 安全 数据库
ctfshow-WEB-web6
ctf.show WEB模块第6关是一个SQL注入漏洞,注入点是单引号字符型注入,并且过滤了空格,我们可以使用括号()或者注释/**/绕过
410 0
ctfshow-WEB-web6
|
存储 边缘计算 人工智能
从中心走向边缘——深度解析云原生边缘计算落地痛点
云计算发展史,就是虚拟化技术的发展史。近 20 年来云计算与互联网相互促进高速发展,中心云技术成为全社会通用的基础设施。随着物联网、人工智能等技术的不断发展,尤其是产业互联网发展落地,中心云计算开始相形见绌,分散式边缘计算在当下被重新寄予厚望。如果中心云计算是由技术创新驱动的,那么边缘计算一定是业务价值驱动的。
1842 4
|
存储 机器学习/深度学习 编解码
阿里云服务器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