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: ...
随机推荐
- AtCoder Regular Contest 069 D - Menagerie 枚举起点 模拟递推
arc069.contest.atcoder.jp/tasks/arc069_b 题意:一堆不明身份的动物排成一圈,身份可能是羊或狼,羊一定说实话,狼一定说假话.大家各自报自己的两边是同类还是不同类, ...
- scrum项目冲刺_day02总结
摘要:今日完成任务. 1.appUI页面完成 2.图像识别正在进行 总任务: 一.appUI页面(已完成) 二.首页功能: 1.图像识别功能 2.语音识别功能 3.垃圾搜索功能 4.相关新闻爬取 三. ...
- nuxt.js同路由跳转参数不同,mounted不执行时
watch: { '$route'(to, from) { if (to.fullPath !== from.fullPath) { this.$nextTick(() => { // 不加th ...
- Linux系列(2) - 命令提示符
命令提示符 起始符 [root@localhost ~]# root:当前登录用户 localhost:主机名 ~:当前所在目录(家目录);管理员为 /root ,user用户为 /home/user ...
- css selector regexp css选择器 正则表达式 css 参考手册
jQuery 选择元素 a.text-success, a.text-danger, a.text-primary, a.text-info $("a[class^=text-]" ...
- 一文让你掌握软件测试工程师SQL面试题
数据结构说明 已知有如下4张表: 学生表:student(学号,学生姓名,出生年月,性别) 成绩表:score(学号,课程号,成绩) 课程表:course(课程号,课程名称,教师号) 教师表:teac ...
- 定要过python二级 第10套
第一部分 第一题 1. int* 字符串 =几个东西 2. 此题的最开始的疑惑 (1)01 02 03 怎么产生 for 循环 (2)<<< 这个怎么产生 (3)<这个&l ...
- springweb项目自定义拦截器修改请求报文头
面向切面,法力无边,任何脏活累活,都可以从干干净净整齐划一的业务代码中抽出来,无非就是加一层,项目里两个步骤间可以被分层的设计渗透成筛子. 举个例子: 最近我们对接某银行接口,我们的web服务都是标准 ...
- Windows 11正式版来了,下载、安装教程、一起奉上!
Windows 11正式版已经发布了,今天给大家更新一波Win11系统的安装方法,其实和Win10基本一样,有多种方法. 安装Win11前请先查看电脑是否支持Win11系统,先用微软自家的PC H ...
- Linux下Electron loadURL报错 ERR_FAILED(-2) Not allowed to load local resource
Linux下Electron loadURL报错 ERR_FAILED(-2) Not allowed to load local resource 背景 使用electron-vue的时候,窗体创建 ...