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的APCu扩展
想必大家都使用过 memcached 或者 redis 这类的缓存系统来做日常的缓存,或者用来抗流量,或者用来保存一些常用的热点数据,其实在小项目中,PHP 也已经为我们准备好了一套简单的缓存系统,完 ...
- nuxt.js同路由跳转参数不同,mounted不执行时
watch: { '$route'(to, from) { if (to.fullPath !== from.fullPath) { this.$nextTick(() => { // 不加th ...
- Java基础系列(30)- 命令行传参
命令行传参 有时候你希望运行一个程序的时候再传递给它消息.这就要靠传递命令行参数main()函数实现 package method; public class CommandLine { public ...
- Java基础系列(9)- 数据类型扩展及常见面试题
整数拓展 // 整数拓展: 进制 二进制0b 十进制 八进制0 十六进制0x // 同一个数字在不同进制中,结果是不同的,进制换算 int i = 10; int i2 = 010; // 八进制 i ...
- Java基础系列(3)- HelloWorld详解
HelloWorld 1.新建一个java文件 文件后缀名为.java Hello.java [注意点]系统可能没有显示文件后缀名,我们需要手动打开 2.编写代码 public class Hello ...
- 疏忽Bug
仅供自己留存备份 错误: vector不是模板 解决: 头文件未包含 头文件: #include <vector> using namespace std; 错误: 多字节字符 ...
- CSS 奇技淫巧 | 妙用混合模式实现文字镂空波浪效果
本文将介绍一个小技巧,通过混合模式 mix-blend-mode 巧妙的实现文字的镂空波浪效果. 起因 一日,一群友私聊问我.如何使用 CSS 实现下述效果,一个文字的波浪效果: 我当时想都没想,就回 ...
- 分析 ajax 请求并抓取 “今日头条的街拍图”
今日头条抓取页面: 分析街拍页面的 ajax 请求: 通过在 XHR 中查看内容,获取 url 链接,params 参数信息,将两者进行拼接后取得完整 url 地址.data 中的 article_u ...
- 小白学习Python英语基础差怎么办,都帮你想好拉!看这里
运算符与随机数 1.module:模块 2.sys(system):系统 3.path:路径 4.import:导入 5.from:从- 定义函数与设定参数 1.birthday:出生日期 2.yea ...
- IdentityServer4[3]:使用客户端认证控制API访问(客户端授权模式)
使用客户端认证控制API访问(客户端授权模式) 场景描述 使用IdentityServer保护API的最基本场景. 我们定义一个API和要访问API的客户端.客户端从IdentityServer请求A ...