如何腌制一个物体?
这是我的代码:
import pickle
alist = ['here', 'there']
c = open('config.pck', 'w')
pickle.dump(alist, c)
这是我收到的错误:
Traceback (most recent call last):
File "C:\pickle.py", line 1, in ?
import pickle
File "C:\pickle.py", line 6, in ?
pickle.dump(alist, c)
AttributeError: 'module' object has no attribute 'dump'
发生了什么事?我在 windows xp 上使用 python 2.4
Here is the code I have:
import pickle
alist = ['here', 'there']
c = open('config.pck', 'w')
pickle.dump(alist, c)
and this is the error I receive:
Traceback (most recent call last):
File "C:\pickle.py", line 1, in ?
import pickle
File "C:\pickle.py", line 6, in ?
pickle.dump(alist, c)
AttributeError: 'module' object has no attribute 'dump'
whats going on? I am using python 2.4 on windows xp
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要将您的文件命名为 pickle.py。它与同名的 python 标准库模块冲突。所以你的
import pickle
没有获取 python 模块。Don't call your file pickle.py. It conflicts with the python standard libary module of the same name. So your
import pickle
is not picking up the python module.你的代码对我来说效果很好。
问题是您的文件名“pickle.py”使
import pickle
语句尝试从您自己的文件导入而不是主库。重命名您的代码文件。The code you have works fine for me.
The issue is that your filename "pickle.py" is making the
import pickle
statement try to import from your own file instead of the main library. Rename your code file.您的脚本称为 pickle,因此隐藏了标准库中的模块 picke。它导入自身并尝试调用其
dump
函数(当然它没有)。请注意,您很“幸运”,因为您没有陷入无限导入循环(因为导入同一模块两次只会创建对内存中同一模块对象的另一个引用)。
Your script is called pickle and therefore shadows the module picke from the standard library. It imports itself and tries to call its
dump
function (and of course it doesn't have one).Note that you're "lucky" that you don't get kicked into an infinite import loop (because importing the same module twice just creates another reference to the same module object in memory).