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: ...
随机推荐
- php时间区间,优化显示
<?php /** * 类似微信的时间显示 * 规则是:今天的,显示几秒前,几分钟前,几小时前,昨天的显示昨天 上午 XX:XX * 再往前,本周的,显示周几+时间,再往前,本年的,显示月日+时 ...
- Java实现导出Excel
项目中实现的代码: 一.//controller @RequestMapping(value = "/exportAccountManager") public void expo ...
- ECShop 文章添加缩略图功能
为 ECShop 文章添加缩略图 ECShop 文章不包含缩略图比较遗憾,不过它的文章里包含一个附件上传,而且一般不会用到,这样,我们就可以改动一下,让它成为缩略图. 首先在 includes ...
- yum 安装 php 环境
如此简单 第一步: sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7. ...
- c++ 的学习 第二集函数的重载2 namemangling
1. 本质 采用了name mangling或者叫name decoration ✓ C++编译器默认会对符号名(比如函数名)进行改编.修饰,有些地方翻译为"命名倾轧"✓ 重载时 ...
- P3313-[SDOI2014]旅行【树链剖分,线段树】
正题 题目链接:https://www.luogu.com.cn/problem/P3313 题目大意 \(n\)个点的一棵树,每个点有一个颜色和权值,有操作 修改一个点的权值 修改一个点的颜色 询问 ...
- P5666-[CSP-S2019]树的重心【树状数组】
正题 题目链接:https://www.luogu.com.cn/problem/P5666 题目大意 给出\(n\)个点的一棵树,对于每条边割掉后两棵树重心编号和. \(1\leq T\leq 5, ...
- 推荐一款 Python 微服务框架 - Nameko
1. 前言 大家好,我是安果! 考虑到 Python 性能及效率性,Python Web 端一直不温不火,JAVA 和 Golang 的微服务生态一直很繁荣,也被广泛用于企业级应用开发当中 本篇文章 ...
- Pycharm新建模板默认添加作者时间等信息(逼格更高,好像很历害的样子)
在pycharm使用过程中,关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便. 方法如下: 1.打开pycharm,选择File-Settings 2.选择Editor--Color&am ...
- 手动实现 shared_ptr
面试写了一个基础的 scoped_ptr,被面试官要求写 shared_ptr,一时语塞.面试官不断提示我说在现有的基础上实现 shared_ptr 很简单,真的很简单,宛如在不断暗示我 1+1 就是 ...