我有一个带有列表的字典作为值,例如:
numlist = {'Person': ['2342342', '15:05']}
我腌了它。
outfile = open{"log.txt", "wb")
pickle.dump(numlist, outfile)
我取消了它。
infile = open("log.txt", "rb")
pickle.load(infile)
如何将此二进制数据转换回原始格式? (带有变量'name'作为键的字典,以及带有两个项目的列表(数字的变量'number'和当时的'calltime')作为值)?
答案 0 :(得分:2)
好的,我们试一试:
In [22]: import pickle
In [23]: numlist = {'Person': ['2342342', '15:05']}
In [24]: outfile = open("log.txt", "wb")
In [25]: pickle.dump(numlist, outfile)
In [26]: outfile.close()
In [27]: infile = open("log.txt", "rb")
In [28]: pickle.load(infile)
Out[28]: {'Person': ['2342342', '15:05']}
正如你所看到的,我完全回到了我的开始(numlist
)。与代码相比,唯一改变的是我在重新打开它之前关闭outfile
,以确保刷新缓冲区。