python - Pickle:存储变量的内容
问题描述
我将模块泡菜保存在我的目录中。现在,我想将变量 ie Arpan 存储在一个名为 spdict.data 的创建文件中,因此,当我输入如下代码时:
import pickle
Arpan = "My name is Arpan"
fh = open("spdict.data","w")
pickle.dump(Arpan,fh)
fh.close()
我希望变量 Arpan 被存储,但我收到以下错误。
回溯(最后一次调用):文件“C:\Users\Home\AppData\Local\Programs\Python\Python36-32\IDLE scripts for bioinfo book\File handling\test1 reading fasta.py”,第 173 行,在 pickle .dump(Arpan,fh) TypeError: write() argument must be str, not bytes
另外,当我进入程序
import pickle
Arpan = {"One":1,"Two":2,"Three":3}
fh = open("spdict.data","w")
pickle.dump(Arpan,fh)
fh.close()
然后,还会出现以下错误:
回溯(最后一次调用):文件“C:\Users\Home\AppData\Local\Programs\Python\Python36-32\IDLE scripts for bioinfo book\File handling\test1 reading fasta.py”,第 173 行,在 pickle .dump(Arpan,fh) TypeError: write() argument must be str, not bytes
请帮忙..我的python版本是3.6.4
解决方案
编写泡菜时,打开文件进行二进制写入。
fh = open("spdict.data", "wb")
推荐阅读
- python-3.x - Python上的利息计算器
- javascript - WebGL 2.0 错误:glDrawArrays:尝试访问属性 0 中超出范围的顶点
- excel - 添加“.Cells”如何解决运行时错误 13?
- html - CSS - SVG 边框溢出
- encryption - RSA 私钥计算 [MADLIB]
- python - 熊猫 groupby 和计算总和
- matlab - 为 2 维系统建模 LQR 不打印特征值和 dcgain
- java - 如何在每个 textview 之后设置带有 4 个按钮的小部件工具栏
- floating-point - Isabelle 中的浮点和区间算术
- shell - 如何运行序列化的ansible任务