pickle 需要“写”;属性?

发布于 2025-01-12 18:40:09 字数 340 浏览 1 评论 0原文

我使用该程序时的错误:

file must have 'write' attribute

我的程序:

import pickle

inp = input()

if inp == "in":
    f = open ("txt.txt", 'wb')
    pickle.dump(f, inp)
    f.close()

if inp == "out":
    f = open ("txt.txt", "rb")
    print(pickle.load(f))
    f.close()

有谁知道如何解决这个问题吗?

My error when using the program:

file must have 'write' attribute

My program:

import pickle

inp = input()

if inp == "in":
    f = open ("txt.txt", 'wb')
    pickle.dump(f, inp)
    f.close()

if inp == "out":
    f = open ("txt.txt", "rb")
    print(pickle.load(f))
    f.close()

Has anyone an idea how to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-01-19 18:40:09

pickle.dump 函数具有 以下签名

pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)

所以文件对象应该是第二个参数。

所以在你的情况下,你会想要:

pickle.dump(inp, f)

The pickle.dump function has the following signature:

pickle.dump(obj, file, protocol=None, *, fix_imports=True, buffer_callback=None)

So the file object should be the second argument.

So in your case, you would want:

pickle.dump(inp, f)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文