试图在python中腌制列表 [英] Trying to pickle a list in python
本文介绍了试图在python中腌制列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
按照许多资料提供的确切方法,尝试使用pickle在python中保存整数列表,但我仍然遇到相同的错误.这是简化版:
Trying to save a list of integers in python using pickle, following the exact method given to me by many sources, and I'm still encountering the same error. Here's the simplified version:
import pickle
a=[0,4,8,[3,5]]
with open(blah.pickle, 'wb') as b:
pickle.dump(a,b)
我总是收到错误消息:
NameError: name 'blah' is not defined
怎么了?
推荐答案
您需要将其设置为字符串:
You need to make it a string:
import pickle
a=[0,4,8,[3,5]]
with open('blah.pickle', 'wb') as b:
pickle.dump(a,b)
没有引号,Python正在寻找名为blah
的变量,并尝试获取该对象的pickle
属性.由于您从未将blah
定义为变量,因此得到了NameError
.
Without the quotes, Python is looking for the variable called blah
and trying to get the pickle
attribute of that object. Since you have never defined blah
as a variable, you get a NameError
.
这篇关于试图在python中腌制列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文