python write() argument must be str, not bytes
python pickle
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pickle
dic = {
"key" : "111",
"id" : "222",
"value" : 333,
"name" : "nihao",
"age" : 18,
}
file_object = open('./test.pkl', 'w')
pickle.dump(dic,file_object,0)
file_object = open('./test.pkl', 'r')
obj = pickle.load(file_object)
print(obj)
在python2环境中,可以成功写入文件,并且可以读取文件.
输出
{'key': '111', 'age': 18, 'id': '222', 'value': 333, 'name': 'nihao'}
同样的代码在python3环境中就不能够写入成功读取成功
在python3中的输出
Traceback (most recent call last):
File "pktest.py", line 26, in <module>
pickle.dump(dic,file_object,0)
TypeError: write() argument must be str, not bytes
如果想在python3中运行相同的代码
需要在代码读取文件处type加b
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pickle
dic = {
"key" : "111",
"id" : "222",
"value" : 333,
"name" : "nihao",
"age" : 18,
}
file_object = open('./test.pkl', 'wb')
pickle.dump(dic,file_object,0)
file_object = open('./test.pkl', 'rb')
obj = pickle.load(file_object)
print(obj)
这份代码可以在python2和python3都输出
{'id': '222', 'value': 333, 'name': 'nihao', 'key': '111', 'age': 18}
python write() argument must be str, not bytes的更多相关文章
- Python错误TypeError: write() argument must be str, not bytes
2016-07-03 20:51:25 今天使用Python中的pickle存储的时候出现了以下错误: TypeError: write() argument must be str, not byt ...
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...
- TypeError: write() argument must be str, not bytes报错
TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...
- python(61):str 和 bytes 转换
str 和 bytes 转换 b = b"example" # str object s = "example" # str to bytes bytes(s, ...
- TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法
Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...
- 【Python】Python3中的str和bytes
参考文章:Python 3的bytes/str之别 len()函数计算的是str的字符数,如果换成bytes,len()函数就计算字节数 >>> len('ABC') 3 >& ...
- TypeError: write() argument must be str, not bytes
w文件打开以 '二进制' 方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱 ...
- 问题记录2:TypeError: write() argument must be str, not bytes
今天试了下用requests模块的get()方法来下载图片,写入文件的时候不能写入二进制,然后将打开方式改成二进制的就好了. 原因是,f.content的存储方式是二进制,而文件正常打开默认是字符串的 ...
- Python中的编码问题(encoding与decode、str与bytes)
1 引言 在文件读写及字符操作时,我们经常会出现下面这几种错误: TypeError: write() argument must be str, not bytes AttributeError: ...
随机推荐
- 第二十次CSP考试有感
这是第二次参加csp考试了,大二上学期参加了第17次csp,160分.刚刚下午结束了第20次csp,200分. 这次比赛规则和以往不同,以前可以携带纸质书籍和usb,提交上去的答案不能实时出成绩.现在 ...
- 论文解读(PCL)《Prototypical Contrastive Learning of Unsupervised Representations》
论文标题:Prototypical Contrastive Learning of Unsupervised Representations 论文方向:图像领域,提出原型对比学习,效果远超MoCo和S ...
- 机器学习——EM算法
1 数学基础 在实际中,最小化的函数有几个极值,所以最优化算法得出的极值不确实是否为全局的极值,对于一些特殊的函数,凸函数与凹函数,任何局部极值也是全局极致,因此如果目标函数是凸的或凹的,那么优化算法 ...
- 【OWASP TOP10】2021年常见web安全漏洞TOP10排行
[2021]常见web安全漏洞TOP10排行 应用程序安全风险 攻击者可以通过应用程序中许多的不同的路径方式去危害企业业务.每种路径方法都代表了一种风险,这些风险都值得关注. 什么是 OWASP TO ...
- vue跳转路由
1. router-link 1. 不带参数 <router-link :to="{name:'home'}"> <router-link :to=" ...
- Java面向对象系列(6)- 封装详解
封装 该露的露,该藏得藏 我们程序设计要追求"高内聚,低耦合".高内聚就是类得内部数据操作细节自己完成,不允许外部干涉:低耦合:仅暴露少量得方法给外部使用 封装(数据得隐藏) 通常 ...
- 深入HTML5第一天
页面的title一般是30-40个字符:分别为主页,详情页,列表页 keywords:100个字符 description: em是:emphasize: 强调,着重 i:italic斜体的 : ...
- python刷题第三周
以下是本周有所收获的题目 第一题: 第4章-4 验证"哥德巴赫猜想" (20 分) 数学领域著名的"哥德巴赫猜想"的大致意思是:任何一个大于2的偶数总能表示为两 ...
- pandas学习小记
pandas操作整理 导入数据: pd.read_csv(filename):从CSV文件导入数据 pd.read_table(filename):从限定分隔符的文本文件导入数据 pd.read_ex ...
- P4001-[ICPC-Beijing 2006]狼抓兔子【对偶图】
正题 题目链接:https://www.luogu.com.cn/problem/P4001 题目大意 给出一个类似于 的网格图,求起点到终点的最小割. 解题思路 最小割直接跑网络流,然后发现\(di ...