使用pickle将对象存储到文件中时出现 TypeError: write() argument must be str, not bytes
来源:互联网 发布:数据恢复免费版下载 编辑:程序博客网 时间:2024/06/03 14:03
pickle默认操作二进制文件,使用文件函数的时候需要注意,否则出现 TypeError
如下,open
函数参数更改为 wb
可以正常运行
#!/usr/bin/python3# -*- coding: utf-8 -*-# 实现用户的历史记录功能# 使用容量为 n 的队列结构from collections import dequefrom random import randintimport pickleimport os# 队列的初始值,容量# history = deque([], 5)history = deque()if os.path.exists("./history"): history = pickle.load(open("history", "rb"))else: histoty = deque([], 5)# N = randint(0, 100)N = 60def guess(k): if k == N: print('Right') return True if k < N: print(str(k) + " is less-than N") else: print(str(k) + " is greater-than N") return Falsewhile True: line = input('Please input a number: ') if line.isdigit(): k = int(line) history.append(k) if guess(k): break elif line == 'history' or line == "h?": print(list(history))pickle.dump(history, open("history", "wb"))
阅读全文
0 0
- 使用pickle将对象存储到文件中时出现 TypeError: write() argument must be str, not bytes
- TypeError: write() argument must be str, not bytes
- docx2txt TypeError: write() argument must be str, not bytes
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- Python 读写文件 错误TypeError: write() argument must be str, not bytes+
- write() argument must be str, not bytes
- python提示错误TypeError: write() argument must be str, not bytes
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- python-TypeError: write() argument must be str, not numpy.float64
- TypeError: must be str, not bytes
- TypeError: must be str, not bytes
- python3-网络编程-TypeError: send() argument 1 must be bytes or buffer, not str异常
- 关于TypeError: strptime() argument 1 must be str, not bytes解析
- 保存网页TypeError: must be str, not bytes
- Python中报错TypeError: must be str, not bytes
- TypeError: the JSON object must be str, not 'bytes'
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- TypeError: expected bytes, not str
- 用户组建立以及权限的修改练习题
- ABP框架运行注意事项
- Linux目录
- 线程通信之生产者与消费者--小码哥java
- Java集合中Comparable和Comparator辨析
- 使用pickle将对象存储到文件中时出现 TypeError: write() argument must be str, not bytes
- hdu 5983 Pocket Cube(模拟)
- tensorflow Lenet5手写字体识别模型的保存与加载
- Java(3-2)
- C语言实验——温度转换
- 行人再识别:平安城市添利器
- Varnish相关功能实现
- 安卓开发-百度地图-几何图形覆盖物+文字覆盖物+弹出窗覆盖物+地图类型+地图控制和手势
- 关于C语言中的内存分区