腌菜:储存variab的内容物

2024-09-29 18:44:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我把模块pickle保存在我的目录中。 现在,我想将变量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行,in pickle.dump(阿潘,fh) TypeError:write()参数必须是str,而不是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行,in pickle.dump(阿潘,fh) TypeError:write()参数必须是str,而不是bytes

请帮忙。。我的python版本是3.6.4


Tags: 文件importhomeclosedatalocal错误open

热门问题